Skip to content

Commit

Permalink
{import} supports arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 24, 2024
1 parent 4e2e08b commit 70a2088
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Latte/Essential/Nodes/ImportNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,39 @@

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;
use Latte\Compiler\Tag;


/**
* {import "file"}
* {import "file"[, args]}
*/
class ImportNode extends StatementNode
{
public ExpressionNode $file;
public ArrayNode $args;


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;
}


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,
);
}
Expand All @@ -45,5 +50,6 @@ public function print(PrintContext $context): string
public function &getIterator(): \Generator
{
yield $this->file;
yield $this->args;
}
}
35 changes: 35 additions & 0 deletions tests/tags/import.args.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Test: {import ..., args}
*/

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->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')),
);
17 changes: 17 additions & 0 deletions tests/tags/import.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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}'));

0 comments on commit 70a2088

Please sign in to comment.