Jinja2 inline comments

121,516

Solution 1

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.

Solution 2

Now Jinja2 has a comment statement:

{% comment %}

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

{% endcomment %}

Solution 3

I was trying to add comments to Martijn Pieters.

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

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

Share:
121,516

Related videos on Youtube

kimstik
Author by

kimstik

Updated on December 21, 2020

Comments

  • kimstik
    kimstik over 3 years

    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.

    • Ross Ridge
      Ross Ridge over 4 years
      While you're generating assembly, your question doesn't actually seem to be related to that. You could be generating HTML or C++ and it wouldn't change your question.
    • Gabriel Staples
      Gabriel Staples about 4 years
  • kimstik
    kimstik over 11 years
    external comments looks not such pretty in my case :(
  • timss
    timss about 8 years
    "{# .. #} is only meant for disabling part of a template" – does not match the current documentation, where it is also used as {# a comment #}.
  • Martijn Pieters
    Martijn Pieters about 8 years
    @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.
  • kimstik
    kimstik over 3 years
    May I use it within a {{ ... }} statement ?
  • Dan Netoff
    Dan Netoff over 3 years
    @kimstik, sorry I dont uderstand your question. But I am edit my answer, plz see it
  • kimstik
    kimstik over 3 years
    I love your approach. Stay curious and motivated. Explore a little deeper
  • Oren
    Oren over 3 years
    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.
  • Ahmed
    Ahmed almost 3 years
    jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'comment'.
  • old-monk
    old-monk about 2 years
    With ansible, I get AnsibleError: template error ... unknown tag comment.