Hi there
With the change https://github.com/twigphp/Twig/pull/3012 the macro import context structure has changed.
Now, I am facing the issue of having a parent with a macro, which I would like to access from an extending template. Before, I was using {% import _self as me %} to access it. This is not working anymore. Also with the {% from %} syntax it is not working.
What would be the new prefered way to access a parent macro from an extending child template? Is it even possible anymore?
Thanks
Can you share a template that does not work anymore? Via https://twigfiddle.com/?
Your example does not seem to work in any Twig version, right?
Hey @fabpot
It worked until this morning, until I updated Twig to 2.11. It worked before, last week with latest Symfony and Twig worked fine.
Cheers
@nea Have a look at the fiddle that you have provided, it does not. work with any versions. So, your code is probably different.
Hi @fabpot
I am sorry but it worked for ages. In the given twigfiddle here I forgot the brackets at the end of the macro. But it worked before.
The exact template is:
{% extends '@PecMtuDvp/BackendEvent/PhysicalTestPlanning_changeset.html.twig' %}
{% trans_default_domain "PecMtuDvpBundle" %}
{% import _self as me %}
{% block changeset_field_block %}
{% if key in ['progress'] %}
{% set baseTranslationPath = 'pec_mtu_dvp.backendevent.physical_test_planning_progress' %}
{% set baseTranslationDomain = 'PecMtuDvpBundle' %}
{{ me.renderChange(key, entity.oldProgress|default(0), item[1], baseTranslationPath, baseTranslationDomain, []) }}
{% elseif key in ['oldProgress'] %}
{% else %}
{{ parent() }}
{% endif %}
{% endblock %}
and down the extends line there is a template with
{%- macro renderChange(key, oldValue, newValue, baseTranslationPath, baseTranslationDomain, defaultEvents) -%}
{% import _self as me %}
<li>
<strong>
{{ me.getKeyTranslation(key, baseTranslationPath, baseTranslationDomain, defaultEvents) }}
</strong>:
{{ me.renderChangeItem(oldValue, key, baseTranslationPath, baseTranslationDomain, defaultEvents) }}
<i class="fas fa-long-arrow-alt-right"></i>
{{ me.renderChangeItem(newValue, key, baseTranslationPath, baseTranslationDomain, defaultEvents) }}
</li>
{%- endmacro -%}
This is actually live with Symfony 4.2.8 and Twig 2.10 and works flawlessly.
Thanks
I'm not saying that there is no issue, just that the Fiddle you gave me never worked. As you can imagine, I cannot fix something that I don't understand :)
Hey @fabpot
Thanks. I updated the fiddle to even better resemble my current local code: https://twigfiddle.com/ts0vl6 works in 2.10 but not 2.11.
Hope this helps.
Thanks
Not sure this is of any help or even related, but I had to downgrade from 2.11 to 2.10 because of this:
request.CRITICAL: Uncaught PHP Exception Twig\Error\RuntimeError:
"Template "form_div_layout.html.twig" cannot be used as a trait." at
.../vendor/symfony/twig-bridge/Resources/views/Form/bootstrap_base_layout.html.twig
line 1 {"exception":"[object] (Twig\\Error\\RuntimeError(code: 0):
Template \"form_div_layout.html.twig\" cannot be used as a trait. at
.../vendor/symfony/twig-bridge/Resources/views/Form/bootstrap_base_layout.html.twig:1)"} []
Working on a fix :)
Awesome, thanks a lot @fabpot
import calls (and even use from if needed).Not sure this is of any help or even related, but I had to downgrade from 2.11 to 2.10 because of this:
request.CRITICAL: Uncaught PHP Exception Twig\Error\RuntimeError: "Template "form_div_layout.html.twig" cannot be used as a trait." at .../vendor/symfony/twig-bridge/Resources/views/Form/bootstrap_base_layout.html.twig line 1 {"exception":"[object] (Twig\\Error\\RuntimeError(code: 0): Template \"form_div_layout.html.twig\" cannot be used as a trait. at .../vendor/symfony/twig-bridge/Resources/views/Form/bootstrap_base_layout.html.twig:1)"} []
me too.
@fabpot did your fix solves this issue too? It is even related? Thanks
Most helpful comment
Working on a fix :)