Skip to content

Commit 9ad504b

Browse files
committed
SnippetNode, SnippetAreaNode: name can be class constant [Closes nette/latte#346]
1 parent 50ce8a4 commit 9ad504b

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/Bridges/ApplicationLatte/Nodes/SnippetAreaNode.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use Latte\Compiler\Block;
1313
use Latte\Compiler\Nodes\AreaNode;
14+
use Latte\Compiler\Nodes\Php;
15+
use Latte\Compiler\Nodes\Php\Expression;
1416
use Latte\Compiler\Nodes\Php\Scalar;
1517
use Latte\Compiler\Nodes\StatementNode;
1618
use Latte\Compiler\PrintContext;
@@ -34,6 +36,13 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator
3436
{
3537
$node = new static;
3638
$name = $tag->parser->parseUnquotedStringOrExpression();
39+
if (
40+
$name instanceof Expression\ClassConstantFetchNode
41+
&& $name->class instanceof Php\NameNode
42+
&& $name->name instanceof Php\IdentifierNode
43+
) {
44+
$name = new Scalar\StringNode(constant($name->class . '::' . $name->name), $name->position);
45+
}
3746
$node->block = new Block($name, Template::LayerSnippet, $tag);
3847
$parser->checkBlockIsUnique($node->block);
3948
[$node->content, $endTag] = yield;

src/Bridges/ApplicationLatte/Nodes/SnippetNode.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Latte\Compiler\Nodes\AreaNode;
1515
use Latte\Compiler\Nodes\AuxiliaryNode;
1616
use Latte\Compiler\Nodes\Html\ElementNode;
17-
use Latte\Compiler\Nodes\Php\Expression\AssignNode;
18-
use Latte\Compiler\Nodes\Php\Expression\VariableNode;
17+
use Latte\Compiler\Nodes\Php;
18+
use Latte\Compiler\Nodes\Php\Expression;
1919
use Latte\Compiler\Nodes\Php\Scalar;
2020
use Latte\Compiler\Nodes\StatementNode;
2121
use Latte\Compiler\PrintContext;
@@ -49,6 +49,13 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator
4949
$node->block = new Block(new Scalar\StringNode(''), Template::LayerSnippet, $tag);
5050
} else {
5151
$name = $tag->parser->parseUnquotedStringOrExpression();
52+
if (
53+
$name instanceof Expression\ClassConstantFetchNode
54+
&& $name->class instanceof Php\NameNode
55+
&& $name->name instanceof Php\IdentifierNode
56+
) {
57+
$name = new Scalar\StringNode(constant($name->class . '::' . $name->name), $name->position);
58+
}
5259
$node->block = new Block($name, Template::LayerSnippet, $tag);
5360
if (!$node->block->isDynamic()) {
5461
$parser->checkBlockIsUnique($node->block);
@@ -148,7 +155,7 @@ private function printAttribute(PrintContext $context): string
148155
XX,
149156
self::$snippetAttribute,
150157
$this->block->isDynamic()
151-
? new AssignNode(new VariableNode('ʟ_nm'), $this->block->name)
158+
? new Expression\AssignNode(new Expression\VariableNode('ʟ_nm'), $this->block->name)
152159
: $this->block->name,
153160
);
154161
}

tests/Bridges.Latte3/expected/n-snippet.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
final class Template%a% extends Latte\Runtime\Template
44
{
55
public const Blocks = [
6-
'snippet' => ['outer' => 'blockOuter', 'gallery' => 'blockGallery', 'script' => 'blockScript'],
6+
'snippet' => ['outer' => 'blockOuter', 'gallery' => 'blockGallery', 'script' => 'blockScript', 'hello' => 'blockHello'],
77
];
88

99

@@ -29,6 +29,12 @@ public function main(array $ʟ_args): void
2929
echo '>';
3030
$this->renderBlock('script', [], null, 'snippet') /* line %d% */;
3131
echo '</script>
32+
33+
<script';
34+
echo ' id="', htmlspecialchars($this->global->snippetDriver->getHtmlId('hello')), '"';
35+
echo '>';
36+
$this->renderBlock('hello', [], null, 'snippet') /* line 9 */;
37+
echo '</script>
3238
';
3339
}
3440

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

7783
$this->global->snippetDriver->enter('script', 'static') /* line %d% */;
7884
try {
79-
echo LR\Filters::escapeJs('x') /* line 7 */;
85+
echo LR\Filters::escapeJs('x') /* line %d% */;
86+
87+
} finally {
88+
$this->global->snippetDriver->leave();
89+
}
90+
}
91+
92+
93+
/** n:snippet="Test::Foo" on line %d% */
94+
public function blockHello(array $ʟ_args): void
95+
{
96+
extract($this->params);
97+
extract($ʟ_args);
98+
unset($ʟ_args);
99+
100+
$this->global->snippetDriver->enter('hello', 'static') /* line %d% */;
101+
try {
102+
echo LR\Filters::escapeJs('y') /* line %d% */;
80103

81104
} finally {
82105
$this->global->snippetDriver->leave();
83106
}
84107
}
85-
}
108+
}

tests/Bridges.Latte3/n-snippet.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ if (version_compare(Latte\Engine::VERSION, '3', '<')) {
1212
Tester\Environment::skip('Test for Latte 3');
1313
}
1414

15+
class Test
16+
{
17+
public const Foo = 'hello';
18+
}
19+
1520

1621
$latte = new Latte\Engine;
1722
$latte->setLoader(new Latte\Loaders\StringLoader);
@@ -26,6 +31,8 @@ $template = <<<'EOD'
2631
2732
<script n:snippet="script">{='x'}</script>
2833
34+
<script n:snippet="Test::Foo">{='y'}</script>
35+
2936
EOD;
3037

3138
Assert::matchFile(

0 commit comments

Comments
 (0)