Skip to content

Commit

Permalink
SnippetNode, SnippetAreaNode: name can be class constant [Closes nett…
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 2, 2023
1 parent 50ce8a4 commit 9ad504b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/Bridges/ApplicationLatte/Nodes/SnippetAreaNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Latte\Compiler\Block;
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\Php;
use Latte\Compiler\Nodes\Php\Expression;
use Latte\Compiler\Nodes\Php\Scalar;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
Expand All @@ -34,6 +36,13 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator
{
$node = new static;
$name = $tag->parser->parseUnquotedStringOrExpression();
if (
$name instanceof Expression\ClassConstantFetchNode
&& $name->class instanceof Php\NameNode
&& $name->name instanceof Php\IdentifierNode
) {
$name = new Scalar\StringNode(constant($name->class . '::' . $name->name), $name->position);
}
$node->block = new Block($name, Template::LayerSnippet, $tag);
$parser->checkBlockIsUnique($node->block);
[$node->content, $endTag] = yield;
Expand Down
13 changes: 10 additions & 3 deletions src/Bridges/ApplicationLatte/Nodes/SnippetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\AuxiliaryNode;
use Latte\Compiler\Nodes\Html\ElementNode;
use Latte\Compiler\Nodes\Php\Expression\AssignNode;
use Latte\Compiler\Nodes\Php\Expression\VariableNode;
use Latte\Compiler\Nodes\Php;
use Latte\Compiler\Nodes\Php\Expression;
use Latte\Compiler\Nodes\Php\Scalar;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
Expand Down Expand Up @@ -49,6 +49,13 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator
$node->block = new Block(new Scalar\StringNode(''), Template::LayerSnippet, $tag);
} else {
$name = $tag->parser->parseUnquotedStringOrExpression();
if (
$name instanceof Expression\ClassConstantFetchNode
&& $name->class instanceof Php\NameNode
&& $name->name instanceof Php\IdentifierNode
) {
$name = new Scalar\StringNode(constant($name->class . '::' . $name->name), $name->position);
}
$node->block = new Block($name, Template::LayerSnippet, $tag);
if (!$node->block->isDynamic()) {
$parser->checkBlockIsUnique($node->block);
Expand Down Expand Up @@ -148,7 +155,7 @@ private function printAttribute(PrintContext $context): string
XX,
self::$snippetAttribute,
$this->block->isDynamic()
? new AssignNode(new VariableNode('ʟ_nm'), $this->block->name)
? new Expression\AssignNode(new Expression\VariableNode('ʟ_nm'), $this->block->name)
: $this->block->name,
);
}
Expand Down
29 changes: 26 additions & 3 deletions tests/Bridges.Latte3/expected/n-snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
final class Template%a% extends Latte\Runtime\Template
{
public const Blocks = [
'snippet' => ['outer' => 'blockOuter', 'gallery' => 'blockGallery', 'script' => 'blockScript'],
'snippet' => ['outer' => 'blockOuter', 'gallery' => 'blockGallery', 'script' => 'blockScript', 'hello' => 'blockHello'],
];


Expand All @@ -29,6 +29,12 @@ public function main(array $ʟ_args): void
echo '>';
$this->renderBlock('script', [], null, 'snippet') /* line %d% */;
echo '</script>
<script';
echo ' id="', htmlspecialchars($this->global->snippetDriver->getHtmlId('hello')), '"';
echo '>';
$this->renderBlock('hello', [], null, 'snippet') /* line 9 */;
echo '</script>
';
}

Expand Down Expand Up @@ -76,10 +82,27 @@ public function blockScript(array $ʟ_args): void

$this->global->snippetDriver->enter('script', 'static') /* line %d% */;
try {
echo LR\Filters::escapeJs('x') /* line 7 */;
echo LR\Filters::escapeJs('x') /* line %d% */;

} finally {
$this->global->snippetDriver->leave();
}
}


/** n:snippet="Test::Foo" on line %d% */
public function blockHello(array $ʟ_args): void
{
extract($this->params);
extract($ʟ_args);
unset($ʟ_args);

$this->global->snippetDriver->enter('hello', 'static') /* line %d% */;
try {
echo LR\Filters::escapeJs('y') /* line %d% */;

} finally {
$this->global->snippetDriver->leave();
}
}
}
}
7 changes: 7 additions & 0 deletions tests/Bridges.Latte3/n-snippet.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}

class Test
{
public const Foo = 'hello';
}


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
Expand All @@ -26,6 +31,8 @@ $template = <<<'EOD'
<script n:snippet="script">{='x'}</script>
<script n:snippet="Test::Foo">{='y'}</script>

EOD;

Assert::matchFile(
Expand Down

0 comments on commit 9ad504b

Please sign in to comment.