Is a Twig block empty? - Symfony 2 -
i have twig template following block:
{% block dashboard %} {% include "::user_dashboard.html.twig" %} {% endblock dashboard %}
later in template, want set class on div based on whether or not there in block (i.e., default, have include above, children of template may override , empty out).
what had (that worked) ...
{% set _dashboard = block('dashboard') %} {% set _mainwidth = ( _dashboard|trim empty ? "no-dashboard" : "with-dashboard" ) #} <div id="main" class="{{ _mainwidth }}">
the problem here whole dashboard block gets called twice. wouldn't bother me except block renders few controller actions, i.e. ...
{% render "userwidget:userappmenu" %}
... , code in action being called twice. various reasons, not least of performance, messes of stuff in dashboard block.
so, question ... there way tell if block empty without loading twice? there simple i'm missing or possible?
thanks!
edit:
here full template if 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 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> {% 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> {% 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 can see on lines 11 , 15 - both of seem include , process in include.
what this? way block should rendered once, when call block('dashboard')
.
{# @ top of twig #} {% set _dashboard = block('dashboard') %} {# ever include block #} <div> {{ _dashboard|raw }} </div> {# , main #} {% set _mainwidth = ( _dashboard|trim empty ? "no-dashboard" : "with-dashboard" ) #} <div id="main" class="{{ _mainwidth }}">
Comments
Post a Comment