Implement builtin template helper to dump available template variables #174

techtonik opened this Issue Jan 17, 2013 · 4 comments


None yet
2 participants

Smarty template engine has a very convenient {debug} built-in that gives a quick overview of variables available for designer.


It would be nice to have the same stuff in Jinja2 by default


westurner commented Feb 18, 2016

I'd have to check, but I think this should work:

<pre>{{ locals() }}</pre>


@mitsuhiko from :

import jinja2

def get_context(c):
 return c

app.jinja_env.globals['context'] = get_context
app.jinja_env.globals['callable'] = callable

for debugging, e.g. pyramid_debugtoolbar can show the template context if the view function -- appropriately abstracted IMHO -- just returns a context object (e.g. dict, OrderedDict)

use case

My use case here is passing a few args from a CLI utility through to a jinja2 template (where I'd like to define defaults within the template (e.g. for Dockerfiles (without autoescape, of all things))

What I'd like to do is

{% set ENVVAR=context.get('ENVVAR', 'DEFAULT') %}

but, alas, the actual context object is [...]

@westurner, so does it work? I am unable to check it right now, but it looks to me that {{ locals() }} should fail if it contains non-convertible bytes.


westurner commented Apr 13, 2016

  • json.dumps(locals()) won't work without a custom JSONEncoder (with eg repr() for [code objects,])
  • maybe pprint.pformat(locals())? (this would be a convenient builtin)

westurner commented Apr 13, 2016

  • ... autoescape=True would then prevent XSS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment