如何在twig中使用单注释行和多注释行

问题描述 投票:0回答:5
symfony twig
5个回答
10
投票
{# Commented Code in Twig #}

希望对您有帮助。


6
投票

twig 命令关键字是#。在 {# 内部使用并以 #} 结尾。以下是您需要的答案。

    {#%if role=3 %}
<div class="col-md-6">
        <div class="form-group">
           <label class="control-label">&nbsp;</label>
           <select multiple class="form-control"  id="path_attachment" name="path_attachment[]"></select>
        </div>
</div>
{% else %}
<div class="col-md-6"></div>
{% endif %#}

6
投票

评论:)

注释掉一行或多行代码(或一行的一部分)

使用 {# ..... #} 语法。

例如:

单行评论:

{# This will be a comment #}

线路的某些部分:

<p>You can also comment out {# part of a line #}.</p>

多行评论:

{# This will be a 
multi-line
comment. 
#}

注释不仅仅对于编写代码注释有用。您还可以导致代码块不被执行。注释标签之间的任何 Twig 代码都不会被执行或输出。

{# The following code will not be executed and nothing will be outputted
{% if category.posts %}
    This category has posts
{% endif %}
#}

2
投票

您还可以使用 IDE/编辑器配置快捷方式来注释多行。

在 PhpStorm 中,我使用 Ctrl + b 来完成此操作。


0
投票

您还可以使用 html 注释标签

<!--
-->

特别是如果您想注释掉已经包含树枝注释的代码块。 它将忽略现有的 {# #} 标签。 ;)

<!-- {# some existing comment using twig comment tags #} -->
有可能

{#   {# some existing comment using twig comment tags #}  #}
不是

© www.soinside.com 2019 - 2024. All rights reserved.