简体   繁体   English

Jinja2 内嵌注释

[英]Jinja2 inline comments

How can I put comments inside Jinja2 argument list declaration ?如何在 Jinja2 参数列表声明中添加注释?

Everything I have tried gives an error: jinja2.exceptions.TemplateSyntaxError: unexpected char u'#'我尝试过的一切都给出了一个错误: 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.在我的情况下,Jinja2 用作汇编程序的宏预处理器。

Jinja2 has no support for comments within a {{ ... }} statement. Jinja2 不支持{{ ... }}语句中的注释。 You can only use comments outside of such statements, and then only with {# .. #} or ## comment .您只能此类语句之外使用注释,然后只能使用{# .. #}## comment

  • {# .. #} is only meant for disabling part of a template or adding comments outside of other Jinja2 syntax. {# .. #}仅用于禁用模板的一部分或其他 Jinja2 语法之外添加注释。 You can't nest these.你不能嵌套这些。
  • # statement is the equivalent of {% statement %} , if line statements are enabled and so configured . # statement相当于{% statement %} ,如果行语句被启用并如此配置
  • ## comment only works if line statements are enabled, at which point it regarded as a comment. ## comment仅在启用行语句时有效,此时它被视为注释。

Generally, outside of Jinja statements, use comments in the target language instead;通常,在 Jinja 语句之外,使用目标语言中的注释代替; eg <!-- comment --> when generating XML, etc.例如<!-- comment -->生成 XML 等时。

Now Jinja2 has a comment statement:现在 Jinja2 有一个评论声明:

{% comment %}

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

{% endcomment %}

I was trying to add comments to Martijn Pieters.我试图向 Martijn Pieters 添加评论。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM