【问题标题】:How do you comment out in Liquid?您如何在 Liquid 中发表评论?
【发布时间】:2015-01-16 09:49:15
【问题描述】:

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

【问题讨论】:

    标签: syntax liquid comments


    【解决方案1】:

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

    {% comment %} This is a comment in Liquid {% endcomment %}
    

    注释是内联注释还是块注释都没有关系。

    {% comment %}
        This is a block comment in Liquid
    {% endcomment %}
    

    【讨论】:

    • 这是一种非常冗长且不符合人体工程学的语法。我期待能够在{% %} 运行中执行类似/* Fnord */ 行的操作,例如{% elseif /* do the other thing:*/ %}。真可惜。
    • 这不能正常注释掉{% if .. %} ,所以它不仅冗长,而且有点垃圾
    【解决方案2】:

    在液体中,使用评论标签将要评论的文本包含在评论标签内

    {%comment%}
    Text to be commented
    {%endcomment%}
    

    【讨论】:

      【解决方案3】:

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

      输入:

      Anything you put between {% comment %} and {% endcomment %} tags
      is turned into a comment.
      

      输出:

      Anything you put between  tags
      is turned into a comment.
      

      参考文档:Comment tag in Liquid

      【讨论】:

      • 我真的很喜欢你做这个例子的方式,所以它在输入和输出中都具有可读性。
      【解决方案4】:

      在液体中,您使用{% comment %}{% endcomment %} 标签:

      {% comment %} This would be commented out {% endcomment %}
      

      你也可以在块中使用它:

      {% comment %}
          This would also be commented out
      {% endcomment %}
      

      如果 {% comment %}{% endcomment %} 标记会注释任何内容,包括 HTML 元素等:

       {% comment %}
          <div class="commented_out">
          <p>This whole div would be commented out</p>
          </div>
      {% endcomment %}
      

      【讨论】:

        【解决方案5】:

        如果像我一样,您正在寻找一个实际上在评论标签之间“anything”/everything的解决方案(如documentation中所述) ,您可以使用{% 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 %}
        

        什么都不渲染,而

        {% raw %}
            Here is some text that I want displayed but
            {% some_liquid_stuff_that_I_don't_want_parsed %}
        {% endraw %}
        

        将渲染

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

        {% some_liquid_stuff_that_I_don't_want_parsed %}

        关于this GitHub thread的更多信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-12
          • 2017-12-06
          • 2016-08-11
          • 1970-01-01
          • 2014-08-12
          • 1970-01-01
          相关资源
          最近更新 更多