Skip to content

Commit f88777b

Browse files
committed
BlockMacros: supports expression as block/snippet name
1 parent c4c4bf7 commit f88777b

22 files changed

+159
-56
lines changed

‎src/Latte/Macros/BlockMacros.php‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ public function macroInclude(MacroNode $node, PhpWriter $writer)
157157
: PhpHelpers::dump($name);
158158

159159
return $writer->write(
160-
'$this->renderBlock' . ($parent ? 'Parent' : '') . '('
161-
. $phpName
162-
. ', %node.array? + '
163-
. '($this->hasBlock(' . $phpName . ', true) ? get_defined_vars() : $this->params)'
160+
'$this->renderBlock' . ($parent ? 'Parent' : '')
161+
. '($__nm = ' . $phpName . ', %node.array? + '
162+
. '($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params)'
164163
. ($node->modifiers
165164
? ', function ($s, $type) { $__fi = new LR\FilterInfo($type); return %modifyContent($s); }'
166165
: ($noEscape || $parent ? '' : ', ' . PhpHelpers::dump(implode($node->context))))
@@ -267,7 +266,7 @@ public function macroBlock(MacroNode $node, PhpWriter $writer): string
267266
);
268267

269268
if ($this->isDynamic($data->name)) {
270-
$node->closingCode = $writer->write('<?php $this->renderBlock(%word, %raw); ?>', $data->name, $renderArgs);
269+
$node->closingCode = $writer->write('<?php $this->renderBlock($__nm, %raw); ?>', $renderArgs);
271270
return $this->beginDynamicBlockOrDefine($node, $writer, $layer);
272271
}
273272

@@ -362,7 +361,7 @@ private function beginDynamicBlockOrDefine(MacroNode $node, PhpWriter $writer, ?
362361
};
363362

364363
return $writer->write(
365-
'$this->addBlock(%word, %var, [[$this, %var]], %var);',
364+
'$this->addBlock($__nm = %word, %var, [[$this, %var]], %var);',
366365
$data->name,
367366
implode($node->context),
368367
$func,
@@ -449,18 +448,17 @@ private function beginDynamicSnippet(MacroNode $node, PhpWriter $writer): string
449448
};
450449
}
451450
$node->attrCode = $writer->write(
452-
"<?php echo ' {$this->snippetAttribute}=\"' . htmlspecialchars(\$this->global->snippetDriver->getHtmlId(%word)) . '\"' ?>",
451+
"<?php echo ' {$this->snippetAttribute}=\"' . htmlspecialchars(\$this->global->snippetDriver->getHtmlId(\$__nm = %word)) . '\"' ?>",
453452
$data->name
454453
);
455-
return $writer->write('$this->global->snippetDriver->enter(%word, %var); try {', $data->name, SnippetDriver::TYPE_DYNAMIC);
454+
return $writer->write('$this->global->snippetDriver->enter($__nm, %var); try {', SnippetDriver::TYPE_DYNAMIC);
456455
}
457456

458457
$node->closingCode .= "\n</div>";
459458
return $writer->write(
460459
"?>\n<div {$this->snippetAttribute}=\""
461-
. '<?php echo htmlspecialchars($this->global->snippetDriver->getHtmlId(%word)) ?>"'
462-
. '><?php $this->global->snippetDriver->enter(%word, %var); try {',
463-
$data->name,
460+
. '<?php echo htmlspecialchars($this->global->snippetDriver->getHtmlId($__nm = %word)) ?>"'
461+
. '><?php $this->global->snippetDriver->enter($__nm, %var); try {',
464462
$data->name,
465463
SnippetDriver::TYPE_DYNAMIC
466464
);
@@ -481,7 +479,7 @@ public function macroSnippetArea(MacroNode $node, PhpWriter $writer): string
481479

482480
$data->after = function () use ($node, $writer, $data, $block) {
483481
$node->content = $writer->write(
484-
'<?php $this->global->snippetDriver->enter(%word, %var);
482+
'<?php $this->global->snippetDriver->enter(%var, %var);
485483
try { ?>%raw<?php } finally { $this->global->snippetDriver->leave(); } ?>',
486484
$data->name,
487485
SnippetDriver::TYPE_AREA,
@@ -610,6 +608,6 @@ private function generateMethodName(string $blockName): string
610608

611609
private function isDynamic(string $name): bool
612610
{
613-
return strpos($name, '$') !== false;
611+
return strpos($name, '$') !== false || strpos($name, ' ') !== false;
614612
}
615613
}

‎tests/Latte/BlockMacros.dynamicblock.phpt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $template = <<<'EOD'
3939
4040
{block "strip$name"|striptags}<span>hello</span>{/block}
4141
42+
{block rand() < 5 ? a : b} expression {/block}
4243
EOD;
4344

4445
Assert::matchFile(

‎tests/Latte/BlockMacros.ifset.block.phpt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Assert::same('<?php if ($this->hasBlock("block")) { ?>', $compiler->expandMacro(
2222
Assert::same('<?php if ($this->hasBlock($foo)) { ?>', $compiler->expandMacro('ifset', '#$foo')->openingCode);
2323
Assert::same('<?php if ($this->hasBlock("foo")) { ?>', $compiler->expandMacro('ifset', 'block foo')->openingCode);
2424
Assert::same('<?php if ($this->hasBlock($foo)) { ?>', $compiler->expandMacro('ifset', 'block $foo')->openingCode);
25+
Assert::same('<?php if ($this->hasBlock(("f" . "oo"))) { ?>', $compiler->expandMacro('ifset', 'block "f" . "oo"')->openingCode);
2526
Assert::same(
2627
'<?php if ($this->hasBlock("foo") && $this->hasBlock("block") && isset($item)) { ?>',
2728
$compiler->expandMacro('ifset', 'block foo, block, $item')->openingCode

‎tests/Latte/BlockMacros.import.phpt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ $latte->setLoader(new Latte\Loaders\StringLoader([
1818
{import "inc"}
1919
{include test}
2020
',
21+
'main-dynamic' => '
22+
{import "i" . "nc"}
23+
{include test}
24+
',
2125
'inc' => '
2226
outer text
2327
{define test}
@@ -34,3 +38,8 @@ Assert::match(
3438
'Test block',
3539
trim($latte->renderToString('main'))
3640
);
41+
42+
Assert::match(
43+
'Test block',
44+
trim($latte->renderToString('main-dynamic'))
45+
);

‎tests/Latte/BlockMacros.include.block.phpt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ $latte->setLoader(new Latte\Loaders\StringLoader([
2323
'main7' => '{define block}[block {$var}]{/define} {var $name = block} before {include block $name, var => 1} after',
2424

2525
'main8' => '{define block}<b>block {$var}</b>{/} before {include block, var => 1|striptags} after',
26+
'main9' => '{define block}block {$var}{/} before {include block true ? "block", var => 2} after',
2627
]));
2728

2829
Assert::match(
@@ -63,3 +64,8 @@ Assert::match(
6364
' before block 1 after',
6465
$latte->renderToString('main8')
6566
);
67+
68+
Assert::match(
69+
' before block 2 after',
70+
$latte->renderToString('main9')
71+
);

‎tests/Latte/BlockMacros.snippet.dynamic.phpt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,19 @@ Assert::matchFile(
2929
__DIR__ . '/expected/BlockMacros.snippet.dynamic.phtml',
3030
$latte->compile($template)
3131
);
32+
33+
34+
$template = <<<'EOD'
35+
{snippet outer}
36+
{foreach array(1,2,3) as $id}
37+
{snippet 'inner-' . $id}
38+
#{$id}
39+
{/snippet}
40+
{/foreach}
41+
{/snippet outer}
42+
EOD;
43+
44+
Assert::matchFile(
45+
__DIR__ . '/expected/BlockMacros.snippet.dynamic2.phtml',
46+
$latte->compile($template)
47+
);

‎tests/Latte/expected/BlockMacros.define.args1.phtml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ final class Template%a% extends Latte\Runtime\Template
1616
}
1717
echo '
1818
a) ';
19-
$this->renderBlock('test', [1] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
19+
$this->renderBlock($__nm = 'test', [1] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
2020
echo '
2121
2222
2323
b) ';
24-
$this->renderBlock('outer', ($this->hasBlock('outer', true) ? get_defined_vars() : $this->params), 'html');
24+
$this->renderBlock($__nm = 'outer', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
2525
echo '
2626
2727
';
2828
$var1 = 'outer';
2929
echo 'c) ';
30-
$this->renderBlock('test', ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
30+
$this->renderBlock($__nm = 'test', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
3131
echo '
3232
3333
d) ';
34-
$this->renderBlock('test', [null] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
34+
$this->renderBlock($__nm = 'test', [null] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
3535
return get_defined_vars();
3636
}
3737

@@ -53,7 +53,7 @@ d) ';
5353
public function blockOuter(array $__args): void
5454
{
5555
extract($__args);
56-
$this->renderBlock('test', ['hello'] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
56+
$this->renderBlock($__nm = 'test', ['hello'] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
5757

5858
}
5959

‎tests/Latte/expected/BlockMacros.define.phtml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ final class Template%a% extends Latte\Runtime\Template
1717
return get_defined_vars();
1818
}
1919
echo "\n";
20-
$this->renderBlock('test', ['var' => 20] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
20+
$this->renderBlock($__nm = 'test', ['var' => 20] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
2121
echo "\n";
22-
$this->renderBlock('true', ($this->hasBlock('true', true) ? get_defined_vars() : $this->params), 'html');
22+
$this->renderBlock($__nm = 'true', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
2323
return get_defined_vars();
2424
}
2525

‎tests/Latte/expected/BlockMacros.define.typehints.phtml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Template%a% extends Latte\Runtime\Template
1515
return get_defined_vars();
1616
}
1717
echo "\n";
18-
$this->renderBlock('test', [1] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
18+
$this->renderBlock($__nm = 'test', [1] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
1919
return get_defined_vars();
2020
}
2121

‎tests/Latte/expected/BlockMacros.dynamicblock.html‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616

1717

1818
hello
19+
20+
expression

0 commit comments

Comments
 (0)