I have set a default variable in my view (Twig template). But when I try to override it inside controller it is not happening. This is my view,
<div class="content-wrapper">
{% if has_header|default(true) == true %}
<!-- Header code -->
{% endif %}
</div>
This is my controller,
return $this->render('index.html.twig', [
'has_header' => false
]);
But unfortunately even I added the has added 'has_header' to false it still runs header code. It would be great if someone can help.
The problem here is that
twig
compiles your code as follows:As you can see, your variable is passed down the function
_twig_default_filter
Reading further in the source you can see the problem lays in the function
twig_test_empty
TLDR Twig's filter
default
also kicks in onfalse
To solve this issue u would need to change your code toNot the actual solution but found a workaround for that,,,
TWIG
Controller
using strings instead of booleans is not the correct way but think it's simpler.