syrusakbary / pyjade

Jade template system for Django, Jinja2 and Mako
MIT License
699 stars 122 forks source link

Set data/locals for processing #276

Open danr opened 7 years ago

danr commented 7 years ago

I cannot find a function to set the locals for processing jade. I'm attaching the workaround I'm using for now below (a little test included), but I'm surprised that this does not seem to be in the API out of the box, so I'm reporting this as a missing feature.

import pyjade
from pyjade.ext.html import HTMLCompiler

def process_jade(src, data={}):
    parser = pyjade.parser.Parser(src)
    block = parser.parse()
    compiler = HTMLCompiler(block, pretty=True)
    compiler.global_context.update(data)
    return compiler.compile()

print(process_jade("""
!!! 5
html
  body
    ul
      each val in my_list
        li= val
""", dict(my_list=[1, 2, 3])))

Thanks!