简体   繁体   English

Twig块是空的吗? - Symfony 2

[英]Is a Twig block empty? - Symfony 2

I have a twig template with the following block: 我有一个带有以下块的树枝模板:

{% block dashboard %}
    {% include "::user_dashboard.html.twig" %}
{% endblock dashboard %}

Later in that template, I want to set a class on a div based on whether or not there is anything in that block (ie, by default, it will have the include above, but children of this template may override it and empty it out). 稍后在该模板中,我想根据该块中是否有任何内容在div上设置一个类(即,默认情况下,它将包含上面的包含,但此模板的子项可能会覆盖它并将其清空)。

What I had (that somewhat worked) was ... 我的(有点工作)是......

{% set _dashboard = block('dashboard') %}
{% set _mainWidth = ( _dashboard|trim is empty ? "no-dashboard" : "with-dashboard" ) #}
<div id="main" class="{{ _mainWidth }}">

The problem here is that whole dashboard block gets called twice. 这里的问题是整个仪表板块被调用两次。 This wouldn't bother me too much except that block renders a few controller actions, ie ... 除了那个块呈现一些控制器动作,即...之外,这不会让我感到烦恼。

{% render "UserWidget:userAppMenu" %}

... and the code in that action is being called twice. ...并且该动作中的代码被调用两次。 For various reasons, not the least of which is performance, this messes with some of the stuff in that dashboard block. 由于各种原因,其中最重要的是性能,这与仪表板块中的一些内容混淆。

So, my question is ... is there any way to tell if that block is empty without loading it twice? 所以,我的问题是......有没有办法告诉该块是否为空而不加载它两次? Is there something really simple I'm missing or is this even possible? 有什么东西真的很简单,我想念或者这甚至可能吗?

Thanks! 谢谢!

EDIT: 编辑:

Here is my full template if it helps clarify things: 这是我的完整模板,如果它有助于澄清事情:

{% extends '::base.html.twig' %}

{% block layout %}

{% block header %}
    {% include "::header.html.twig" %}
{% endblock header %}

<div id="container" class="row-fluid">

    {% block dashboard %}
        {% include "::user_dashboard.html.twig" %}
    {% endblock dashboard %}

    {% set _dashboard = block('dashboard') %}
    {% set _mainWidth = ( _dashboard|trim is empty ? "no-dashboard" : "with-dashboard" ) %}  
    <div id="main" class="{{ _mainWidth }}">
        <h1 class="page-title">{% block page_title %}{% endblock %}</h1>
        {% block main_filters %}{% endblock %}

        {% if app.session.flashbag.has('message') %}
          <div class="alert alert-block alert-success">
            <ul>
                {% for flashMessage in app.session.flashbag.get('message') %}
                    <li>{{ flashMessage }}</li>
                {% endfor %}
            </ul>
          </div>
        {% endif %}

        {% if app.session.flashbag.has('warning') %}
          <div class="alert alert-block alert-success">
            <ul>
                {% for flashWarning in app.session.flashbag.get('warning') %}
                    <li>{{ flashWarning }}</li>
                {% endfor %}
            </ul>
          </div>
        {% endif %}

        {% block body %}{% endblock %}

        {% block footer %}
            {% include "::footer.html.twig" %}
        {% endblock footer %}
    </div>

</div>

{% endblock layout %}

Here you can see on lines 11 and 15 - both of those actually seem to include and process what is in that include. 在这里你可以看到第11和第15行 - 这两个实际上似乎包括和处理包含的内容。

What about this? 那这个呢? This way the block should only be rendered once, when you call block('dashboard') . 这样,当您调用block('dashboard')时,块只应呈现一次。

{# at top of twig #}
{% set _dashboard = block('dashboard') %}

{# where ever you include your block #}
<div>
    {{ _dashboard|raw }}
</div>

{# and your main #}
{% set _mainWidth = ( _dashboard|trim is empty ? "no-dashboard" : "with-dashboard" ) #}
<div id="main" class="{{ _mainWidth }}">

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

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