46
votes

Imagine I have something like this in my twig template

{% block posLeft %}
   -----
{%endblock%}

Is there any way to check for existance of the posLeft block without calling to:

block("posLeft") 

And check the return value of the posBlock to varify the existance. I am a newbie in Symfony2 + Twig.

6
Why would you want to do that? Seems to me that you're adding logic into your views that probably belongs in your controller. If you want to display certain content based on some variable then simply inject that variable into your view and then display different blocks as needed.lifo

6 Answers

82
votes

You can solve it like this, if you want to display a certain block only if it has content. Hope, this is what you're looking for.

Example index.html.twig

{% set _block = block('dynamic') %}
{% if _block is not empty %}
    {{ _block|raw }}
{% endif %}

Example part.html.twig

{% extends "index.html.twig" %}

{% block dynamic %}
    Block content goes here.
{% endblock %}
18
votes

You can do it like this:

{% if block('posLeft') %}
  ...
{% endif %}

But it is not efficient if you need output of rendered block. So if you will need block output you should assign it to variable in the first place and then do assertions

16
votes

The other answers here do not work for twig 2.1 (I've not tested on ~2.0), so here's a small update:

{% if block('dynamic') is defined %}
    {{ block('dynamic')|raw }}
{% endif %}

Note that the line to render the block is not:

{% block dynamic %}
    {#  this wont work  #}
{% endblock %}

This wont work because the block will be rendered during compile, and so the test will return true that it exists (as its tested at runtime). So you need to render the block with {{ block('dynamic')|raw }} instead as this doesn't actually define the block in the template.

11
votes

First check, which Twig version you are using inside your symfony project, because the answers here are only for Twig 1.

If you are using Twig 2 you are lucky. According to the Twig documentation you can use the defined test to check if the block exists in the current template context.

{% if block("dynamic") is defined %}
   ...
{% endif %}

I have written a little TwigExtension to check if the block gets called inside the if statement and it seems like Twig realy only checks if the block exsists and does not call it.

The link to the docs: http://twig.sensiolabs.org/doc/2.x/functions/block.html

If your are using Twig 1, the old answer at https://stackoverflow.com/a/13806784/6458657 is still correct.

8
votes

Twig 2.x

{{ (block("posLeft")) ?? '' }}

If you want to display a block if it's defined or not in one line. Might be a little kludgy but satisfies my needs without a bunch of if..then logic.

1
votes

Just want to provide another example which worked for me.

<body
{%  if block('ngapp') is not empty  %}ng-app="{% block ngapp %}{% endblock %}"{% endif %}
>

This allows me in child templates declare {% block ngapp 'myApp' %} and have it displayed within parent.

This was needed because on some pages I was bootstrapping Angular manually via (angular.bootstrap('moduleName', rootElement)) and Angular does not like empty ng-app='' directive and breaks in weird ways.