You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just tried to use a different twig trait depending on whether a form variable exists or not.
Below is a simplified version of my template structure.
I'm building a "Create" form that can run inside a modal window driven by Turbo Frames, but the same page also works outside of the modal window, with different surrounding HTML markup, when loaded directly, not via the modal.
My Symfony controller is aware of whether or not the request came from a modal window, and sets a modal variable to either true, or false.
base.html.twig
<html>
<head> ... something shared by a lot of scripts - no markup, just metadata ...</head>
<body>
{%block shell %}{%endblock %}
</body>
</html>
{% block shell %}
<turbo-frame id="modal-content">
{{ form_start(form) }}
<div class="header">some header contents</div>
{% block body %}
By default we just render the form, but people can override the block body, if they need something more specialised
{{ form_rest(form) }}
{% endblock body %}
<button type="submit">
{{ form_end(form) }}
</turbo-frame>
... some additional modal markup ...
{% endblock shell %}
create.html.twig
{% extends'form-base.html.twig' %}
{% blockbody %}
... some custom form body markup - not important.
{% endblock %}
Now, let's not get into whether I'm doing it wrong or not - I know I could extend different base templates etc., my point here is that I tried to use the {% use %} trait and it behaved differently than expected:
The core of what's wrong: Even though my debug toolbar dumped FIRST and not the SECOND, twig used the {% block shell %} present in modal-form.html.twig, while it should have used standard-form.html.twig.
Docs (https://twig.symfony.com/doc/3.x/tags/use.html) say that if multiple traits define the same block, the last one wins. When I commented the {% use 'modal-form.html.twig' %} out, the correct template showed up.
Conclusion in what I think is wrong:
When Twig parses the templates, it ignores the if/else construct around the use tag.
Is that expected behaviour? I think it should be at least documented?
I also tried {% use (form is defined ? '_stimulus/shell/modal-form.html.twig' : '_stimulus/shell/modal.html.twig') %}, which would work for example with {% include %}, but I got an error The template references in a "use" statement must be a string., suggesting the template path at that point was not resolved, further confirming my suspition.
The text was updated successfully, but these errors were encountered:
{% use %} is a compile-time feature, while {% if %} is a runtime one. So it is expected that you cannot wrap a {% use %} tag inside a if to make it conditional.
That's exactly the same than for {% extends %} or {% block %} (a block inside a if is rendered conditionally, but it is always defined).
When I try to use anything else than a plain string, I get that The template references in a "use" statement must be a string. - which is the only reason why I even tried to wrap the use in if/else.
I often extend a template using a variable and I thought there would be no difference in doing that with use? If it can't be done, maybe it's worth mentioning in the docs.
Version context:
Twig version: v3.3.10
Twig Bridge version: 6.0.8
Symfony version: 6.0.8
PHP version: 8.1.5
What's happening:
I just tried to use a different twig trait depending on whether a form variable exists or not.
Below is a simplified version of my template structure.
modal
variable to either true, or false.base.html.twig
form-base.html.twig
standard-form.html.twig
modal-form.html.twig
create.html.twig
Now, let's not get into whether I'm doing it wrong or not - I know I could extend different base templates etc., my point here is that I tried to use the {% use %} trait and it behaved differently than expected:
Consider the base template:
The core of what's wrong: Even though my debug toolbar dumped FIRST and not the SECOND, twig used the {% block shell %} present in
modal-form.html.twig
, while it should have usedstandard-form.html.twig
.Docs (https://twig.symfony.com/doc/3.x/tags/use.html) say that if multiple traits define the same block, the last one wins. When I commented the
{% use 'modal-form.html.twig' %}
out, the correct template showed up.Conclusion in what I think is wrong:
When Twig parses the templates, it ignores the if/else construct around the
use
tag.Is that expected behaviour? I think it should be at least documented?
I also tried
{% use (form is defined ? '_stimulus/shell/modal-form.html.twig' : '_stimulus/shell/modal.html.twig') %}
, which would work for example with{% include %}
, but I got an errorThe template references in a "use" statement must be a string.
, suggesting the template path at that point was not resolved, further confirming my suspition.The text was updated successfully, but these errors were encountered: