Implement builtin template helper to dump available template variables #174

Open
techtonik opened this Issue Jan 17, 2013 · 4 comments

Comments

Projects
None yet
2 participants

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

Smarty_debug_console

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

Contributor

westurner commented Feb 18, 2016

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

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

... https://stackoverflow.com/questions/3398850/how-to-get-a-list-of-current-variables-from-jinja-2-template

@mitsuhiko from https://stackoverflow.com/a/13757358/188833 :

import jinja2

@jinja2.contextfunction
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.

Contributor

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)
Contributor

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