Jinja2 内联注释

2023-02-20Python开发问题
4

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

问题描述

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

How can I put comments inside Jinja2 argument list declaration ?

我尝试过的一切都会出错:jinja2.exceptions.TemplateSyntaxError: unexpected char 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 语法之外添加注释.你不能嵌套这些.
  • # statement 等价于 {% statement %},如果 行语句已启用并进行配置.
  • ## comment 仅在启用 line 语句时才有效,此时它被视为注释.
  • {# .. #} 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 语句之外,使用目标语言的注释来代替;例如 生成XML时等

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

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11