是否可以仅在树枝的某些情况下打开无空间模式?

时间:2012-05-19 21:35:46

标签: twig template-engine

我想做这样的事情:

{% if compress %}{% spaceless %}{% endif %}
...
{% if compress %}{% endspaceless %}{% endif %}

我正在尝试将['compress' => true]从PHP传递到模板以启用spaceless模式。但它会导致错误;模板标签需要正确嵌套。

是否有任何技术可以让我从PHP开启/关闭无空间?

2 个答案:

答案 0 :(得分:2)

您必须重新构建模板才能执行此类操作。

{% import _self as example %}
{% macro stuff(obj) %}
  output stuff with {{ obj.name }}, etc...
{% endmacro %}

{% if compress %}
  {% spaceless %}
    {{ example.stuff(bla) }}
  {% endspaceless %}
{% else %}
    {{ example.stuff(bla) }}
{% endif %}

使用macros可以避免重复内容。顶部的import语句很重要,所以不要忘记它。

答案 1 :(得分:-2)

page.twig:

{% block page %}
page content
{% endblock %}

index.twig:

{% extends 'page.twig' %}
{% block page %}
    {% if compress %}
        {% spaceless %}
           {{ parent() }}
        {% endspaceless %} 
    {% else %}
        {{ parent() }}
    {% endif %}
{% endblock %}