def new_context(self, vars=None, shared=False, locals=None): """Create a new :class:`Context` for this template. The vars provided will be passed to the template. Per default the globals are added to the context. If shared is set to `True` the data is passed as it to the context without adding the globals. `locals` can be a dict of local variables for internal usage. """ return new_context(self.environment, self.name, self.blocks, vars, shared, self.globals, locals)
def do_lookup(options, classifier, jerakia, devices): renderer = TemplateRenderer(basepath="", classifier=classifier, jerakia=jerakia, devices=devices) scope = classifier.scope(options.device) result = renderer._lookup( new_context(renderer.env, "internal lookup", {}, vars=dict(device=options.device, **scope)), options.namespace, options.key) if result is not None: print(yaml.dump(result, sort_keys=False))
def extract_template(request, template, context=None): """ Extracts the values used in the template along with the rendered versions of those values. To be used with the output of the `compile_to_js.py` template compiler. """ def get_context(): c = {} if context is None else context.copy() try: from django.template.context import get_standard_processors for processor in get_standard_processors(): c.update(processor(request)) except ImportError: pass return c if isinstance(template, jinja2.environment.Template): raise Exception("Pre-compiled templates may not be used with the " "Jinja2JS extractor.") if env.loader is None: raise TypeError("The environment does not have a configured loader.") source, filename, uptodate = env.loader.get_source(env, template) ast = env._parse(source, name=None, filename=filename) compiler = JSONVisitor(env, name=None, filename=filename) compiler.visit(ast) gen_python = compiler.stream.getvalue() compiled = compile(gen_python, filename, "exec") namespace = {} exec compiled in namespace jinj_context = new_context(env, None, blocks={}, vars=get_context(), globals=env.globals) namespace["root"](jinj_context, env) vars = jinj_context.vars output = {} for key, var in vars.items(): if not isinstance(var, (int, float, long, str, unicode, bool)): var = bool(var) output[key] = var return output
def new_context(self, vars = None, shared = False, locals = None): return new_context(self.environment, self.name, self.blocks, vars, shared, self.globals, locals)
def new_context(self, vars=None, shared=False, locals=None): return new_context(self.environment, self.name, self.blocks, vars, shared, self.globals, locals)