你如何评论Liquid?

Mar*_*erl 46 syntax commenting liquid

在Liquid模板语言中注释的正确方法是什么?

Mar*_*erl 83

Liquid中,您可以使用{% comment %}{% endcomment %}标签注释掉:

{% comment %} This is a comment in Liquid {% endcomment %}
Run Code Online (Sandbox Code Playgroud)

注释是内联还是块注释无关紧要.

{% comment %}
    This is a block comment in Liquid
{% endcomment %}
Run Code Online (Sandbox Code Playgroud)

  • 这是令人惊讶的冗长且非人机工程学的语法。我期望能够在`{%%}`运行中执行`/ * Fnord * /`行,例如`{%elseif / *做另一件事:* /%}`。真可惜 (7认同)
  • 正确答案是下面只有少数赞成票的答案:/sf/answers/4540751351/ (2认同)

Luk*_*uke 21

如果像我一样,您正在寻找一种实际注释掉注释标签之间的“任何内容”/所有内容的解决方案(如文档中所述),则可以使用该标签(如果您不想要任何内容​​,则{% raw %}与该标签结合使用){% comment %}在浏览器中呈现)。

例子:

{% comment %}
    {% raw %}
        Here is some text that I don't want displayed and
        {% some_liquid_stuff_that_I_don't_want_parsed %}
    {% endraw %}
{% endcomment %}
Run Code Online (Sandbox Code Playgroud)

不会渲染任何东西。

相比之下,

{% raw %}
    Here is some text that I want displayed but
    {% some_liquid_stuff_that_I_don't_want_parsed %}
{% endraw %}
Run Code Online (Sandbox Code Playgroud)

将渲染

这是我想要显示的一些文本,但是

{% some_liquid_stuff_that_I_don't_want_parsed %}

尽管

{% comment %}
    Here is some text that I don't want displayed but
    {% some_liquid_stuff_that_will_be_parsed %}
{% endcomment %}
Run Code Online (Sandbox Code Playgroud)

可能会导致语法错误或 Liquid 异常,具体取决于注释标签内 Liquid 的有效性。

这成为问题的一个例子是一些正在进行的代码已被注释掉:

{% comment %}
    {% if some test %}
         some stuff to render
    {% elsif... %}
         unfinished code...
{% endcomment %}
Run Code Online (Sandbox Code Playgroud)

(在这种情况下,您可能会遇到未完成的 if 语句错误。)

有关此 GitHub 线程的更多信息。


Bol*_*olt 8

从 Liquid 5.4.0 开始,您将能够使用不需要结束标签的简短内嵌注释!语法是:

{% # This is a new inline comment! %}
Run Code Online (Sandbox Code Playgroud)

与其他标签一样,您可以添加连字符来删除其周围的空格:

{%- # This is a new inline comment without whitespace! -%}
Run Code Online (Sandbox Code Playgroud)

甚至使用多行:

{%- 
################################
#  This is a really big block  #
################################ 
-%}
Run Code Online (Sandbox Code Playgroud)

合并后的 PR中提供了更多信息。


okc*_*sed 7

Liquid 允许您使用{% comment %}{% endcomment %}标签将未渲染的代码留在 Liquid 模板中。

输入:

Anything you put between {% comment %} and {% endcomment %} tags
is turned into a comment.
Run Code Online (Sandbox Code Playgroud)

输出:

Anything you put between  tags
is turned into a comment.
Run Code Online (Sandbox Code Playgroud)

参考文档:Liquid 中的注释标签

  • 我真的很喜欢你做这个例子的方式,所以它在输入和输出中都具有可读性。 (2认同)