From 930973e58eb62d1de1ae4746f60b18d4300f2f8b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 19 May 2024 23:13:49 +0200 Subject: [PATCH] {import} supports arguments --- src/Latte/Essential/Nodes/ImportNode.php | 10 +++++-- tests/tags/import.args.phpt | 35 ++++++++++++++++++++++++ tests/tags/import.nodes.phpt | 17 ++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/tags/import.args.phpt diff --git a/src/Latte/Essential/Nodes/ImportNode.php b/src/Latte/Essential/Nodes/ImportNode.php index cc2331b3f..923656222 100644 --- a/src/Latte/Essential/Nodes/ImportNode.php +++ b/src/Latte/Essential/Nodes/ImportNode.php @@ -9,6 +9,7 @@ namespace Latte\Essential\Nodes; +use Latte\Compiler\Nodes\Php\Expression\ArrayNode; use Latte\Compiler\Nodes\Php\ExpressionNode; use Latte\Compiler\Nodes\StatementNode; use Latte\Compiler\PrintContext; @@ -16,11 +17,12 @@ /** - * {import "file"} + * {import "file"[, args]} */ class ImportNode extends StatementNode { public ExpressionNode $file; + public ArrayNode $args; public static function create(Tag $tag): static @@ -28,6 +30,8 @@ public static function create(Tag $tag): static $tag->expectArguments(); $node = new static; $node->file = $tag->parser->parseUnquotedStringOrExpression(); + $tag->parser->stream->tryConsume(','); + $node->args = $tag->parser->parseArguments(); return $node; } @@ -35,8 +39,9 @@ public static function create(Tag $tag): static public function print(PrintContext $context): string { return $context->format( - '$this->createTemplate(%node, $this->params, "import")->render() %line;', + '$this->createTemplate(%node, %node? + $this->params, "import")->render() %line;', $this->file, + $this->args, $this->position, ); } @@ -45,5 +50,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { yield $this->file; + yield $this->args; } } diff --git a/tests/tags/import.args.phpt b/tests/tags/import.args.phpt new file mode 100644 index 000000000..930312e96 --- /dev/null +++ b/tests/tags/import.args.phpt @@ -0,0 +1,35 @@ +setLoader(new Latte\Loaders\StringLoader([ + 'main' => <<<'XX' + + {import "inc", val: 123} + {include test} + + XX, + 'inc' => <<<'XX' + + outer text + {define test} + Test {$val} + {/define} + + XX, +])); + +Assert::match( + 'Test 123', + trim($latte->renderToString('main')), +); diff --git a/tests/tags/import.nodes.phpt b/tests/tags/import.nodes.phpt index e8566c1d9..f0250ac0d 100644 --- a/tests/tags/import.nodes.phpt +++ b/tests/tags/import.nodes.phpt @@ -17,5 +17,22 @@ Assert::match(<<<'XX' Import: Variable: name: var + Array: Fragment: XX, exportTraversing('{import $var}')); + + +Assert::match(<<<'XX' + Template: + Fragment: + Import: + Variable: + name: var + Array: + ArrayItem: + Identifier: + name: param + String: + value: val + Fragment: + XX, exportTraversing('{import $var, param: val}'));