Jinja2在线评论

如何在 Jinja2参数列表声明中放置注释?

我试过的所有方法都出错了: 例外。 TemplateSyntaxError: 意外的 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用作汇编程序的宏预处理器。

143286 次浏览

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.

I was trying to add comments to Martijn Pieters.

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

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

Now Jinja2 has a comment statement:

{% comment %}


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


{% endcomment %}