Skip to content

Jinja2 Delimiters

Jinja2 uses special delimiters to embed logic into HTML.

1) Print an expression: {{ ... }}{{ ... }}

Use this to output variables or expressions:

<p>User: {{ username }}</p>
<p>2 + 2 = {{ 2 + 2 }}</p>
<p>User: {{ username }}</p>
<p>2 + 2 = {{ 2 + 2 }}</p>

2) Template statements: {% ... %}{% ... %}

Use this for control flow (if/for), blocks, macros:

{% if is_logged_in %}
  <p>Welcome back!</p>
{% else %}
  <p>Please log in.</p>
{% endif %}
{% if is_logged_in %}
  <p>Welcome back!</p>
{% else %}
  <p>Please log in.</p>
{% endif %}

3) Comments: {# ... #}{# ... #}

These do not appear in final HTML:

{# This is a comment #}
{# This is a comment #}

Common pitfall

If you accidentally use {{ ... }}{{ ... }} for an ifif statement, you’ll get confusing output/errors.

Remember:

  • {{ }}{{ }} outputs
  • {% %}{% %} controls

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did