I have a variable in my code that is buried deep in some legacy code. Rather than spend all day searching for it, I'd like to just print out the variable from within the jinja template. Is that possible?
I tried {% print var %}
, but that didn't seem to do anything.
The syntax for outputting variables is {{var}}, {% %} is for blocks and other directives. However, it sounds like the variable wasn't passed to the template. Check for that.
If you're doing a lot of debugging, try Flask-DebugToolbar, it'll print out all the variables that got passed to your template so you don't have to muck around with print statements like this. Useful stuff.
You need context-processors.
Example to put on your .py file:
@app.context_processor
def get_legacy_var():
return dict(get_legacy_var=your_get_legacy_var_function())
Then on your template:
{{ get_legacy_var }}
This will call Python during template generation, will get the value for your variable, and return it to the template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With