Using macros with dynamic name using dot operator does not work in 3.x
While upgrading twig v1.30 to v3.22.0 i encountered some problem using macros from an external file and calling them by using a variable value as dynamic name.
When using V1.30, you could use the attributes(macroalias, macroname, parameters) function as described in twig attributes function documentation. The page states as well, that this function is deprecated as of twig 3.15 and should be substituted with the dot operator.
When using the attributes function in v3.22 it results in no output (but no error whatsoever).
from what i understand you should be able to use dynamic names for the methods there as well, so instead of using twigmacros.mymacro(params) you should be able to set a variable with the value mymacro and then call twigmacros.(mymacroname)(params).
unfortunately this does not work but produces an error, which i don't really get right:
Fatal error: Uncaught Twig\Error\SyntaxError: Expected name or number, got value "(" of type punctuation. in includes/templates/twig_v3_template.twig:9
Stack trace:
#0 vendor/twig/twig/src/ExpressionParser.php(421): Twig\ExpressionParser->parseSubscriptExpression(Object(Twig\Node\Expression\NameExpression))
#1 vendor/twig/twig/src/ExpressionParser.php(291): Twig\ExpressionParser->parsePostfixExpression(Object(Twig\Node\Expression\NameExpression))
#2 vendor/twig/twig/src/ExpressionParser.php(177): Twig\ExpressionParser->parsePrimaryExpression()
#3 vendor/twig/twig/src/ExpressionParser.php(72): Twig\ExpressionParser->getPrimary()
#4 vendor/twig/twig/src/Parser.php(130): Twig\ExpressionParser->parseExpression()
#5 vendor/twig/twig/src/Parser.php(83): Twig\Parser->subparse(NULL, false)
#6 vendor/twig/twig/src/Environment.php(522): Twig\Parser->parse(Object(Twig\TokenStream))
#7 vendor/twig/twig/src/Environment.php(550): Twig\Environment->parse(Object(Twig\TokenStream))
#8 vendor/twig/twig/src/Environment.php(380): Twig\Environment->compileSource(Object(Twig\Source))
#9 vendor/twig/twig/src/Environment.php(342): Twig\Environment->loadTemplate('__TwigTemplate_...', 'twig_v3_templat...')
#10 twigtest.php(38): Twig\Environment->load('twig_v3_templat...')
#11 {main}
thrown in includes/templates/twig_v3_template.twig on line 9
Am I misreading the documentation about the dot operator and that you are not able to dynamically call macros with a name in a variable oram I using it the wrong way?
It really worked well with the attributes function and i would really be surprised if this functionality would have been removed somehow without even been mentioned somewhere in the documentation.
Files to reproduce the issue:
File with macros: twig_v3_macros.twig
Template that imports/uses the macros: twig_v3_template.twig
PHP File using the template and rendering: twigtest.php