Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement builtin template helper to dump available template variables #174

Closed
techtonik opened this issue Jan 17, 2013 · 16 comments
Closed

Implement builtin template helper to dump available template variables #174

techtonik opened this issue Jan 17, 2013 · 16 comments
Milestone

Comments

@techtonik
Copy link

@techtonik techtonik commented Jan 17, 2013

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

@westurner
Copy link
Contributor

@westurner 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 [...]

@techtonik
Copy link
Author

@techtonik techtonik commented Apr 13, 2016

@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
Copy link
Contributor

@westurner 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
Copy link
Contributor

@westurner westurner commented Apr 13, 2016

  • ... autoescape=True would then prevent XSS

@exhuma
Copy link

@exhuma exhuma commented Aug 15, 2017

This would be a boon to have when extending templates of a third-party app. I am currently using hieroglyph to create slide-shows, which builds on sphinx, and thus Jinja2.

I've extended the default hieroglyph theme, and what I would like to find out is if the template contains some variable which tells me if I'm on a slide or not. Maybe the variable is there, maybe it isn't. So I am currently poking inside the hieroglyph source-code to find what I need.

Being able to simply dump all the available variables directly from within the template (that is, without modifying any Python code) would make this so much quicker and easier. So, let's say being able to do something like this would be nice:

{% extends "!layout.html %}

{# ... or any other sensible name #}
{% dump_variables() %}

{% block body %}
Hello World!
super()
Goodbye World
{% endblock %}

This way, I could introspect whatever hieroglyph offers me while developing the new theme.

@ShaheedHaque
Copy link
Contributor

@ShaheedHaque ShaheedHaque commented Jan 5, 2018

I created an extension for this purpose, it can be found at https://github.com/niwinz/django-jinja/files/1607805/jinja_extensions.py.txt

@techtonik
Copy link
Author

@techtonik techtonik commented Jan 6, 2018

@ShaheedHaque any screens?

@ShaheedHaque
Copy link
Contributor

@ShaheedHaque ShaheedHaque commented Jan 6, 2018

@techtonik
Copy link
Author

@techtonik techtonik commented Jan 7, 2018

@ShaheedHaque screen would be really nice to compare with Smarty output from the first post.

@ShaheedHaque
Copy link
Contributor

@ShaheedHaque ShaheedHaque commented Jan 8, 2018

image

As requested. You'll see that the contents are organised (and sorted) by context variables, filters and tests. Also, here is a zoomed-in view on a screen with a few more "interesting" context variables. See how the limit to depth=3 is enough to show a bit of detail, without incurring the possibility of overly nested output since, I believe, the most requested thing is to know exactly what (top level) context variables are present and not the full depth of any structures:

image

You'll also likely see that it is trivial to change/extend the details of what is shown (or it will be, for somebody who knows how the parser works...I'm not in that blessed circle :-)).

@techtonik
Copy link
Author

@techtonik techtonik commented Jan 8, 2018

Looks useful. So why not to fill a PR?

@ShaheedHaque
Copy link
Contributor

@ShaheedHaque ShaheedHaque commented Jan 8, 2018

I'd be happy to do that given some guidance on where in the ninja codebase the new code should live (I'm not familiar with this project).

@techtonik
Copy link
Author

@techtonik techtonik commented Jan 9, 2018

Me neither, but https://github.com/pallets/jinja/blob/master/jinja2/ext.py seems like a good start.

@ShaheedHaque
Copy link
Contributor

@ShaheedHaque ShaheedHaque commented Jan 14, 2018

See PR #798.

@davidism
Copy link
Member

@davidism davidism commented Oct 5, 2019

Will be available as a new extension in 2.11.

@davidism davidism closed this as completed Oct 5, 2019
@westurner
Copy link
Contributor

@westurner westurner commented Oct 5, 2019

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants