Macro arguments using leading/tailing underscore (BC introduced in v3.15)
Hi. I discovered a side effect after upgrading to the latest version, related to this line in PR 4391. For me, it's a BC, which may affect others as well. The usage of macro arguments having leading/tailing underscores worked well until 3.15.
Example:
{% macro testing(_message) %}
The message: '{{ _message }}'.
{% endmacro %}
{{ _self.testing('Hello World!') }}
Output:
3.14: The message: 'Hello World!'.
3.15: The message: ''.
Compiled template:
In the referred line of the PR, underscores are trimmed off from arguments as they are assigned onto $context. _message becomes message, but the original _message is requested in the escape-method later. That's an error.
[...]
class __TwigTemplate_[...] extends Template
[...]
// line 1
public function macro_testing($_message = null, ...$varargs): string|Markup
{
$macros = $this->macros;
$context = [
"message" => $_message,
"varargs" => $varargs,
] + $this->env->getGlobals();
$blocks = [];
return ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros, $blocks) {
// line 2
yield "The message: '";
yield $this->env->getRuntime('Twig\Runtime\EscaperRuntime')->escape(($context["_message"] ?? null), "html", null, true);
yield "'.
";
yield from [];
})())) ? '' : new Markup($tmp, $this->env->getCharset());
}
I'm curious how I should see the code change. If this is the intention, maybe a trigger_deprecation should be called while evaluating the macro arguments.
I just wanted to let you know - the affected templates will be fixed on my side.
@fabpot what was the reason to trim _ in the names ?
File doc/coding_standards.rst:
Line 91 | Old: * Use lower cased and underscored variable names:
Line 91 | New: * Use snake case for all variable names (provided by the application and created in templates):
[....]
Added: * Use snake case for all function/filter/test names:
....Okay! This explanation has clarified my issue.
The compiled template looks much cleaner without the __ wrap per macro argument:
3.14: public function macro_testing($___message__ = null, ...$__varargs__)
3.15: public function macro_testing($_message = null, ...$varargs): string|Markup
According to the doc changes, _message as macro argument is deprecated.
I think the reason for trim is RESERVED_NAMES which are wrapped using '_'.
Hello. I have an error too when I upgraded 3.14.2 to 3.15
An exception has been thrown during the rendering of a template ("Warning: Undefined variable $_v0"). on each rendered of easyadmin's crud routes (4.15.1)
Like the missing variable is $_v0 with an underscore, I think is may be in relation with this issue. It seems to appear with |trans filter.
#4475 should fix this issue
Same issue! Not working! twig/twig - 3.16.0 Twig, the flexible, fast, and secure template language for PHP
List installed packages:
You probably also need to update the Symfony Twig bridge (the patch has not been released yet though).
Same @Seeman13 , I upgrade to 3.16.0 and I have always the error
If it’s the same, then you also have to tote the Twig bridge as I wrote before.
I feel me stupid but... I removed my vendor dir and I did composer update : same problem.
I do composer require symfony/twig-bridge and the result is Nothing to install, update or remove ; so I don't understand when you say "You probably also need to update the Symfony Twig bridge". How I do this ?
It hasn't been released yet. You need to install the dev version or wait for the release.
I installed the newest version Symfony with the newest packages! I had installed EasyAdmin: symfony composer req easycorp/easyadmin-bundle. I am running to mysite.loc/admin and see this error! I tried done downgrade / upgrade different versions of twig, twig-bridge. It don't work!
I don't understand why release such releases so that the functionality that worked would fall apart? Which would lead to the impossibility of using the engine. And how do the developers test what they do? Or do they not check errors? And how much time should we wait for this miracle patch? Is this being done on purpose?
@Seeman13 having bugs in a release is never an intended outcome. We are all humans and mistake happens.
And as said many times before, the patch already exists. If you install the dev version of symfony/twig-bridge, you will get the patched version.
@Seeman13 Yes, I introduced the bug intentionally in symfony/symfony#58706 to see how many people use Symfony for free and even don't give back by testing beta releases with the application but rage after installing the stable release about stupid maintainers that don't invest enough of their free time to provide you with free software that you don't do anything for.
@Seeman13Да, я намеренно добавил ошибку в symfony/symfony#58706 , чтобы посмотреть, сколько людей используют Symfony бесплатно и даже не отдают должное, тестируя бета-версии приложения, но злятся после установки стабильной версии на тупых разработчиков, которые не тратят достаточно своего свободного времени, чтобы предоставить вам бесплатное программное обеспечение, за которое вы ничего не делаете.
In my practice, I have worked with many different CMS and Frameworks, including those written in other programming languages. And I don't remember that after installing the latest version of some engine, it was immediately impossible to use it because of the presence of bugs in it. And what was my surprise that only after install the environment for creating an application, I immediately encountered the fact that I could not use it.
And this bug did appear yesterday? Are you busy? No time to fix it? Of course, it is easier to write a lot of messages on forums about the bug than to correct mistakes.
And as for payment... Well, make the engine paid and set a price for it - will pay if we deem it necessary! What's the problem? So what should pay for? For the fact that the standard functionality does not work out of the box?
I would recommend everybody read carefully the comments to better understand the situation before adding yet another comment that does not help. I'm locking this conversation now as there is nothing else to say. The bug was in Symfony and it has been fixed there (but no release yet there).