so heres the use case:
twig 2.2
symfony 3.2
In a base template, I'm only rendering a block if its defined (not 'not emtpy')
{% if block('left_sidebar') is defined %}
<div class="col-md-2">
{{- block('left_sidebar') -}}
</div>
<div class="col-md-10">
{% else %}
<div class="col-md-12">
{% endif %}
For the above to work the block cant be defined at all (which is entirely designed). The following renders the block anyway, and I cant' figure out why.
{% if not is_granted('IS_FULLY_AUTHENTICATED') %}
{% block left_sidebar %}
{% include ':blocks:block__login.html.twig' %}
{% endblock %}
{% endif %}
I'm wondering if this is'nt working because of the same reason that the base template code does work. That is that the blocks are compiled before runtime, and the conditional statements are executed at runtime.
Can anyone please confirm that this I'm right? Or correct me if I'm wrong?
edit
I've tried forcing the result of the condition to both true and false, and the block is rendered in either case.
so, to wrap this up, as it seems to be a problem occurring in a few places, my suspicions are correct in that its a compile/runtime issue.
Blocks are compiled and because the if statement is at runtime one cant control the other.
heres the github issue thread if anyone wants more info.
The block()
function appears to return a falsey value if nothing or only white space was output in it, so you can wrap the block in a truthiness test and in the child template make sure it is empty if you don't want it to show. Something like this worked for me:
base.html.twig:
{% if block('left_sidebar') %}
<div class="col-md-2">
{% block left_sidebar %}{% endblock %}
</div>
<div class="col-md-10">
{% else %}
<div class="col-md-12">
{% endif %}
index.html.twig
{% block left_sidebar %}
{% if not is_granted('IS_FULLY_AUTHENTICATED') %}
{% include ':blocks:block__login.html.twig' %}
{% endif %}
{% endblock %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With