How can I comment in a Jinja directive?

I guess everyone's going to find that out via documentation but just in case, I'll leave it here as well:

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

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

Since you are using Jinja 2.2+ then you can use whatever your environment configures for line_comment_prefix (part of the line statements feature). This feature must be enabled by the application that Jinja is embedded in (for example, in Flask, this is done by setting app.jinja_options['line_comment_prefix'] = "whatever#you$want").

app = Flask(__name__)

app.jinja_options['line_statement_prefix'] = '#'
app.jinja_options['line_comment_prefix'] = '#::'

Then you can write a template that uses line comments:

{% set mylist = [
  "first item",
  "another item", #:: needed for raisins - see #12345
  "a third item"
] %}

If you are using Jinja 2.1 or lower then those versions do not support inline comments. However, you can use a comment block:

{#
BUG: Added "another item" because of raisins.
Don't remove it until #12345 is fixed
#}
{% set mylist = [
  "item 1",
  "another item",
  "yet another item",
] %}

Tags:

Jinja2