Skip to content

Commit 2c0820a

Browse files
committed
Latte: n:snippet works with <script>
1 parent 88b5a83 commit 2c0820a

File tree

3 files changed

+59
-28
lines changed

3 files changed

+59
-28
lines changed

src/Bridges/ApplicationLatte/Nodes/SnippetNode.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,32 @@ public static function create(Tag $tag, TemplateParser $parser): \Generator
8585

8686
public function print(PrintContext $context): string
8787
{
88-
$dynamic = $this->block->isDynamic();
89-
if (!$dynamic) {
88+
if (!$this->block->isDynamic()) {
9089
$context->addBlock($this->block);
9190
}
9291

93-
$snippetContent = $context->format(
92+
if ($this->htmlElement) {
93+
try {
94+
$inner = $this->htmlElement->content;
95+
$this->htmlElement->content = new AuxiliaryNode(fn() => $this->printContent($context, $inner));
96+
return $this->content->print($context);
97+
} finally {
98+
$this->htmlElement->content = $inner;
99+
}
100+
} else {
101+
return <<<XX
102+
echo '<div {$this->printAttribute($context)}>';
103+
{$this->printContent($context, $this->content)}
104+
echo '</div>';
105+
XX;
106+
}
107+
}
108+
109+
110+
private function printContent(PrintContext $context, AreaNode $inner): string
111+
{
112+
$dynamic = $this->block->isDynamic();
113+
$res = $context->format(
94114
<<<'XX'
95115
$this->global->snippetDriver->enter(%node, %dump) %line;
96116
try {
@@ -103,34 +123,20 @@ public function print(PrintContext $context): string
103123
$dynamic ? new AuxiliaryNode(fn() => '$ʟ_nm') : $this->block->name,
104124
$dynamic ? SnippetDriver::TypeDynamic : SnippetDriver::TypeStatic,
105125
$this->position,
106-
$this->htmlElement->content ?? $this->content,
126+
$inner,
107127
);
108128

109-
if (!$dynamic) {
110-
$this->block->content = $snippetContent;
111-
$snippetContent = $context->format(
112-
'$this->renderBlock(%node, [], null, %dump) %line;',
113-
$this->block->name,
114-
Template::LayerSnippet,
115-
$this->position,
116-
);
129+
if ($dynamic) {
130+
return $res;
117131
}
118132

119-
if ($this->htmlElement) {
120-
try {
121-
$saved = $this->htmlElement->content;
122-
$this->htmlElement->content = new AuxiliaryNode(fn() => $snippetContent);
123-
return $this->content->print($context);
124-
} finally {
125-
$this->htmlElement->content = $saved;
126-
}
127-
} else {
128-
return <<<XX
129-
echo '<div {$this->printAttribute($context)}>';
130-
{$snippetContent}
131-
echo '</div>';
132-
XX;
133-
}
133+
$this->block->content = $res;
134+
return $context->format(
135+
'$this->renderBlock(%node, [], null, %dump) %line;',
136+
$this->block->name,
137+
Template::LayerSnippet,
138+
$this->position,
139+
);
134140
}
135141

136142

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

Lines changed: 24 additions & 1 deletion
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'],
6+
'snippet' => ['outer' => 'blockOuter', 'gallery' => 'blockGallery', 'script' => 'blockScript'],
77
];
88

99

@@ -23,6 +23,12 @@ public function main(array $ʟ_args): void
2323
echo '">';
2424
$this->renderBlock('gallery', [], null, 'snippet') /* line %d% */;
2525
echo '</div>
26+
27+
<script';
28+
echo ' id="', htmlspecialchars($this->global->snippetDriver->getHtmlId('script')), '"';
29+
echo '>';
30+
$this->renderBlock('script', [], null, 'snippet') /* line %d% */;
31+
echo '</script>
2632
';
2733
}
2834

@@ -59,4 +65,21 @@ public function blockGallery(array $ʟ_args): void
5965
$this->global->snippetDriver->leave();
6066
}
6167
}
68+
69+
70+
/** n:snippet="script" on line %d% */
71+
public function blockScript(array $ʟ_args): void
72+
{
73+
extract($this->params);
74+
extract($ʟ_args);
75+
unset($ʟ_args);
76+
77+
$this->global->snippetDriver->enter('script', 'static') /* line %d% */;
78+
try {
79+
echo LR\Filters::escapeJs('x') /* line 7 */;
80+
81+
} finally {
82+
$this->global->snippetDriver->leave();
83+
}
84+
}
6285
}

tests/Bridges.Latte3/n-snippet.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ $template = <<<'EOD'
2424
2525
<div n:snippet="gallery" class="{=class}"></div>
2626
27+
<script n:snippet="script">{='x'}</script>
28+
2729
EOD;
2830

2931
Assert::matchFile(

0 commit comments

Comments
 (0)