'Nested print statements in Jinja template
I am creating a link in a Jinja2 template using Flask and I am running into a problem. I want to access a variable passed to the template within a code block, but I am unsure how to do it or if it is even possible. What I am trying to do:
<a href="{{ url_for('myRoute', varToBePassed = {{templateVar}} ) }}" >
So basically I want to pass a query parameter to a variable using the url_for
function in a template, but it doesn't like the nested double curly braces. I could alternatively just do it like:
<a href="/myRoute/{{templateVar}}" >
which works, but I feel like I could run into problems in the future if I have to move things in the file structure.
Is there a way to nest curly braces in Jinja templates? Or another way to pass variables to code blocks in Jinja? I looked into macros, but it seemed like I would run into the same problems with nested curly braces in it, as well.
Solution 1:[1]
Just use the template variable directly:
a href="{{ url_for('myRoute', varToBePassed = templateVar ) }}" >
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alvaro Fuentes |