I found out that these {{}}
can directly display variables inside of html, no js required, when built up like the following:
given the URL https://myurl.com/link?key=HFEQ67fWE2
<p>{{ key }}</p>
will set the <p>.textContent
to the value of the arg key
HFEQ67fWE2
Can someone please help me how to use this feature consistently?
I was trying to understand how exactly to use this feature because after a bit more work on my project it suddenly stopped working.
2
Answers
They have no meaning in HTML.
Various template languages (which can be used to output HTML) use them, including Jinja2 (the default template language for Flask, which you tagged).
The documentation is here.
The
{{ }}
in Flask are used for template variable interpolation.Flask uses the Jinja2 template engine under the hood. Jinja2 uses
{{ }}
as the syntax for variables and expressions that get replaced with values when a template is rendered.In a Flask view function, you can pass variables to a template using the
render_template()
method. For example:Then in the template (index.html), you can use
{{ name }}
to insert the value of the name variable:When the template is rendered,
{{ name }}
will be replaced with ‘John’ in this case.However, double curly braces allow you to insert any variables, expressions, filters etc in a template.
Check this out: