118

How can I put comments inside Jinja2 argument list declaration ?

Everything I have tried gives an error: jinja2.exceptions.TemplateSyntaxError: unexpected char u'#'

{{ Switch('var',
    [('1', 'foo'),    #  comment 1
     ('2', 'bar'),    ## comment 2
     ('3', 'rum'),    {# comment 3 #}
     ]) }}


{% macro Switch(var, caselist) %}
    {% for case, action in caselist%}
        CMP  {{var}} {{case}} 
        JNE  {{LABEL}}
        {{action}}
        JMP  {{LABELF}}
{{LABEL}}:  NOP
    {%- endfor %}
{{LABELF}}: NOP
{%- endmacro -%}

In my case Jinja2 is used as macro preprocessor for assembler.

kimstik
  • 1,611
  • 2
  • 15
  • 14

3 Answers3

210

Jinja2 has no support for comments within a {{ ... }} statement. You can only use comments outside of such statements, and then only with {# .. #} or ## comment.

  • {# .. #} is only meant for disabling part of a template or adding comments outside of other Jinja2 syntax. You can't nest these.
  • # statement is the equivalent of {% statement %}, if line statements are enabled and so configured.
  • ## comment only works if line statements are enabled, at which point it regarded as a comment.

Generally, outside of Jinja statements, use comments in the target language instead; e.g. <!-- comment --> when generating XML, etc.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    external comments looks not such pretty in my case :( – kimstik Nov 26 '12 at 09:56
  • 5
    "`{# .. #}` is only meant for disabling part of a template" – does not match the current documentation, where it is also used as `{# a comment #}`. – timss Mar 07 '16 at 10:24
  • 5
    @timss: That sentence should be read *in the context of this question*, where the OP used `{# comment 3 #}` within a block. Yes, `{# ... #}` are used for commenting, including commenting out (disabling) part of a template. – Martijn Pieters Mar 07 '16 at 11:02
  • 1
    If to use then it will be parsed in webbrowser. Chrome -> Sources; Firefox -> Debugger. Potentially insecure in case comments are well described. https://stackoverflow.com/questions/59178354/why-does-django-parse-tags-in-commented-html-code – Alexred Oct 10 '22 at 19:00
  • @Alexred: yes, that's why they are *outside of Jinja statements*. They are part of the output, which can be any (textual) format. – Martijn Pieters Oct 12 '22 at 12:30
23

I`m sorry, but this statement deprecated!

Now Jinja2 has a comment statement:

{% comment %}

    <html code/>
    {% some other statements %}
    {{ some.values }}

{% endcomment %}
Dan Netoff
  • 317
  • 2
  • 5
  • May I use it within a {{ ... }} statement ? – kimstik Dec 15 '20 at 09:54
  • @kimstik, sorry I dont uderstand your question. But I am edit my answer, plz see it – Dan Netoff Dec 15 '20 at 14:26
  • I love your approach. Stay curious and motivated. Explore a little deeper – kimstik Dec 16 '20 at 10:08
  • 6
    For some reason, I still don't have {% comment %}, but then I have used: {% if False %} instead, and I guess it is the same idea. – Oren Jan 14 '21 at 12:54
  • 2
    jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'comment'. – Ahmed Jun 23 '21 at 21:24
  • With ansible, I get AnsibleError: template error ... unknown tag comment. – old-monk Mar 22 '22 at 16:59
  • https://jinja.palletsprojects.com/en/3.1.x/templates/#comments doesn't seem to support this comment tag being available. also, i get the exception as the two prior uh... commentors ;-) when i try to use this tag. – boar May 08 '23 at 20:49
11

I was trying to add comments to Martijn Pieters.

{% .. %} = {# .. #}

{{ .. }} = {# .. #} (same as above)

vallentin
  • 23,478
  • 6
  • 59
  • 81
Paul Thach
  • 131
  • 1
  • 2