mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 03:46:39 +00:00
Fix null coalescing operator with imported macros
This commit is contained in:
@@ -29,6 +29,16 @@ class MacroReferenceExpression extends AbstractExpression implements SupportDefi
|
|||||||
parent::__construct(['template' => $template, 'arguments' => $arguments], ['name' => $name], $lineno);
|
parent::__construct(['template' => $template, 'arguments' => $arguments], ['name' => $name], $lineno);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __clone()
|
||||||
|
{
|
||||||
|
// The template node must not be deep-cloned because its name is
|
||||||
|
// lazily generated during compilation and must stay in sync with
|
||||||
|
// the AssignTemplateVariable that populates the $macros array.
|
||||||
|
$template = $this->nodes['template'];
|
||||||
|
parent::__clone();
|
||||||
|
$this->nodes['template'] = $template;
|
||||||
|
}
|
||||||
|
|
||||||
public function compile(Compiler $compiler): void
|
public function compile(Compiler $compiler): void
|
||||||
{
|
{
|
||||||
if ($this->definedTest) {
|
if ($this->definedTest) {
|
||||||
|
|||||||
@@ -150,6 +150,43 @@ class TemplateTest extends TestCase
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getNullCoalesceWithImportedMacroData
|
||||||
|
*/
|
||||||
|
public function testNullCoalesceWithImportedMacro(array $templates, string $expected)
|
||||||
|
{
|
||||||
|
$twig = new Environment(new ArrayLoader($templates));
|
||||||
|
|
||||||
|
$this->assertSame($expected, trim($twig->render('index.twig')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getNullCoalesceWithImportedMacroData(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'from import' => [
|
||||||
|
[
|
||||||
|
'index.twig' => '{% from "helper.twig" import foo %}{{ foo("bar") ?? "" }}',
|
||||||
|
'helper.twig' => '{% macro foo(param) %}{{ param }}{% endmacro %}',
|
||||||
|
],
|
||||||
|
'bar',
|
||||||
|
],
|
||||||
|
'from import with undefined macro falls back' => [
|
||||||
|
[
|
||||||
|
'index.twig' => '{% from "helper.twig" import foo, nonexistent %}{{ nonexistent("bar") ?? "fallback" }}',
|
||||||
|
'helper.twig' => '{% macro foo(param) %}{{ param }}{% endmacro %}',
|
||||||
|
],
|
||||||
|
'fallback',
|
||||||
|
],
|
||||||
|
'from import used multiple times' => [
|
||||||
|
[
|
||||||
|
'index.twig' => '{% from "helper.twig" import foo %}{{ foo("a") ?? "" }}-{{ foo("b") ?? "" }}',
|
||||||
|
'helper.twig' => '{% macro foo(param) %}{{ param }}{% endmacro %}',
|
||||||
|
],
|
||||||
|
'a-b',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function testRenderBlockWithUndefinedBlock()
|
public function testRenderBlockWithUndefinedBlock()
|
||||||
{
|
{
|
||||||
$twig = new Environment(new ArrayLoader());
|
$twig = new Environment(new ArrayLoader());
|
||||||
|
|||||||
Reference in New Issue
Block a user