Comments Not Working in Jinja2

Comments not working in jinja2

Basically, jinja2 is only concerned with finding an evaluating its own blocks, not the structure of the HTML. If you want to exclude a section of your template entirely, you can use jinja2's comment syntax:

{# This is a comment now.
<div class="control-group">
...
</div>
#}

How can I comment in a Jinja directive?

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",
] %}

flask render_template giving error for commented html code

Sure it's normal ... you are trying to render an empty page.

Why do you put in comment all your page ?

Maybe you could have your answer there : Comments not working in jinja2

or there : http://jinja.pocoo.org/docs/2.10/templates/#comments

How can I read comment blocks from a Jinja2 template?

I browsed through jinja's source code and other articles, and there does not seem to be a way to collect jinja2 comments natively. Per Martijin Peter's answer on Jinja2 Inline Comments, the {# #} syntax can be used as inline comments, but they are primarily used for disabling part of the template

Answer

{# .. #} is only meant for disabling part of a template

Comment

... Yes, {# ... #} are used for commenting, including commenting out (disabling) part of a template.

As a work around, you can either use regex (which you've stated you wish to not use) or you can switch to standard HTML comments and use BeautifulSoup. Using BeautifulSoup does support collecting comments natively with ease

template = '''<!--
Comment block content.
-->

{% block main %}
This is the main block. We don't really care about it.
{% endblock %}'''

from bs4 import BeautifulSoup, Comment
soup = BeautifulSoup(template, 'html.parser')
comments = soup.findAll(text=lambda text: isinstance(text, Comment))
print(comments)

>>>['\nComment block content.\n']

jinja2.exceptions.TemplateSyntaxError: Missing end of comment tag

The source of the problem is the inlined, minimized CSS that targets an id, like: {#sidebar{ or {#sidebar.opened+#page-container{. By default Jinja2 uses {# for comments, so it tries to find the closing tag #}. However you can choose a different string for these tags, e.g.:

templateEnv = jinja2.Environment(
loader=templateLoader,
comment_start_string='{=',
comment_end_string='=}',
)

Alternatively, you can put these CSS into an external file.

Inline comment in ansible jinja2 template

Update for 2021 (version 3.0.x)



Use plus sign (+) in tag

As @mdaniel pointed out, there are a few ways to adjust Jinja2's whitespace behavior, but a new option has appeared in 3.0.x's documentation that I believe is easiest way to solve the problem:

... you can manually disable the trim_blocks behavior by
putting a plus sign (+) at the end of a block:

<div>
{% if something +%}
yay
{% endif %}
</div>

Therefore, your specific problem should be easily solved by the insertion of a single plus symbol:

FOO={{BAR}}        {# blah blah blah +#}
SOMETHING=2

And if you want to remove the whitespace which would appear between the variable and the comment, explicitly remove it with a minus symbol:

FOO={{BAR}}        {#- blah blah blah +#}
SOMETHING=2


Use trim_blocks parameter

Since this question tags Ansible, I should point out that you can also modify the behavior by setting the trim_blocks parameter of the ansible.builtin.template module. This has been part of Ansible since version 2.4. The default is yes, but you can set it to no to achieve the desired result.


Hopefully this will help other people who find this question seeking a similar solution.

Why is Jinja code not removed by HTML comments?

Jinja does not have that much knowledge about HTML to parse HTML comments. If you think about Jinja as a template engine for arbitrary text formats (not just HTML), this behavior makes total sense.



Related Topics



Leave a reply



Submit