Jinja2内联评论 [英] Jinja2 inline comments

查看:83
本文介绍了Jinja2内联评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Jinja2参数列表声明中添加注释?

How can I put comments inside Jinja2 argument list declaration ?

我尝试过的所有操作都会产生错误: jinja2.exceptions.TemplateSyntaxError:意外的字符u'#'

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 -%}

在我的情况下,Jinja2用作汇编程序的宏预处理器.

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

推荐答案

Jinja2不支持{{ ... }}语句中的注释.您只能在此类语句的外部中使用注释,然后只能与{# .. #}## comment一起使用.

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

  • {# .. #}仅用于禁用模板的一部分或添加其他Jinja2语法的 outside 注释.您不能嵌套这些.
  • 如果启用了行语句并且
  • {# .. #} 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.

通常,在Jinja语句之外,使用目标语言的注释代替;例如<!-- comment -->生成XML等时

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

这篇关于Jinja2内联评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆