Line Statements

If line statements are enabled by the application, it’s possible to mark aline as a statement. For example, if the line statement prefix is configuredto #, the following two examples are equivalent:

  1. <ul>
  2. # for item in seq
  3. <li>{{ item }}</li>
  4. # endfor
  5. </ul>
  6. <ul>
  7. {% for item in seq %}
  8. <li>{{ item }}</li>
  9. {% endfor %}
  10. </ul>

The line statement prefix can appear anywhere on the line as long as no textprecedes it. For better readability, statements that start a block (such asfor, if, elif etc.) may end with a colon:

  1. # for item in seq:
  2. ...
  3. # endfor

Note

Line statements can span multiple lines if there are open parentheses,braces or brackets:

  1. <ul>
  2. # for href, caption in [('index.html', 'Index'),
  3. ('about.html', 'About')]:
  4. <li><a href="{{ href }}">{{ caption }}</a></li>
  5. # endfor
  6. </ul>

Since Jinja 2.2, line-based comments are available as well. For example, ifthe line-comment prefix is configured to be ##, everything from ## tothe end of the line is ignored (excluding the newline sign):

  1. # for item in seq:
  2. <li>{{ item }}</li> ## this comment is ignored
  3. # endfor