简体   繁体   English

Spaceless不希望在Symfony2 Twig模板中正常工作

[英]Spaceless does not want to work correctly in Symfony2 Twig Template

I expected the following: 我期待以下内容:

{% spaceless %}
    blablabla
        blablabla
            blablabla
        blablabla
    blablabla
{% endspaceless %}

to return the following string: 返回以下字符串:

blablabla blablabla blablabla blablabla blablabla

but in the page source, I see this: 但是在页面源代码中,我看到了这个:

    blablabla
        blablabla
            blablabla
        blablabla
    blablabla

How to do this without any HTML <tags> ? 如何在没有任何HTML <tags>情况下执行此操作?

You can achieve this by using a whitespace control modifier in a twig comment: 您可以通过在树枝注释中使用空白控件修饰符来实现此目的:

{#-  #}blablabla {# -#}
    blablabla {# -#}
            blablabla {# -#}
    blablabla {# -#}
blablabla{# -#}

a 'regular' comment looks like this: {# my comment #} “常规”评论如下:{#my comment#}

adding a '-' will remove the whitespaces before or after the comment (or both): 添加“ - ”将删除注释之前或之后的空格(或两者):

  • remove whitespaces before comment: {#- my comment #} 在评论之前移除空格:{# - my comment#}
  • remove whitespaces after comment: {# my comment -#} 评论后删除空格:{#my comment - #}
  • remove whitespaces before and after: {#- my comment -#} 删除之前和之后的空格:{# - 我的评论 - #}

by the way, this also works for {{- ... -}} and {%- ... -%} 顺便说一句,这也适用于{{ - ... - }}和{% - ... - %}

As explained in the documentation , "Use the spaceless tag to remove whitespace between HTML tags, NOT whitespace within HTML tags or whitespace in plain text:" 文档中所述,“使用无空格标记删除HTML标记之间的空格,而不是 HTML标记中的空格或纯文本中的空格:”

{% spaceless %}
    <div>blablabla</div>
        <div>blablabla</div>
            <div>blablabla</div>
        <div>blablabla</div>
    <div>blablabla</div>
{% endspaceless %}

Will output: 将输出:

<div>blablabla</div><div>blablabla</div><div>blablabla</div><div>blablabla</div><div>blablabla</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM