如何在 Jinja 指令中发表评论?

How can I comment in a Jinja directive?

我有一个 Jinja set 指令如下:

{% set mylist = [
  "item 1",
  "another item",
  "yet another item",
] %}

我想对第二个列表项添加评论。 Jinja 支持吗? 我试过以下方法:

{% set mylist = [
  "item 1",
  "another item",  # My comment
  "yet another item",
] %}

{% set mylist = [
  "item 1",
  "another item",  ## My comment
  "yet another item",
] %}

,但其中 none 有效。我正在使用 Jinja 2.6。

不,Jinja 不支持内嵌注释。但是,您可以使用评论区:

{#
BUG: Added "another item" because of raisins.
Don't remove it until #12345 is fixed
#}
{% set mylist = [
  "item 1",
  "another item",
  "yet another item",
] %}

我想每个人都会通过文档找到答案,但为了以防万一,我也将它留在这里:

Since Jinja 2.2, line-based comments are available as well. For example, if the line-comment prefix is configured to be ##, everything from ## to the end of the line is ignored (excluding the newline sign):

# for item in seq:
    <li>{{ item }}</li>     ## this comment is ignored
# endfor