Skip to content

Commit

Permalink
BlockMacros: supports expression as block/snippet name
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 23, 2020
1 parent c4c4bf7 commit f88777b
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 56 deletions.
24 changes: 11 additions & 13 deletions src/Latte/Macros/BlockMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ public function macroInclude(MacroNode $node, PhpWriter $writer)
: PhpHelpers::dump($name);

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

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

Expand Down Expand Up @@ -362,7 +361,7 @@ private function beginDynamicBlockOrDefine(MacroNode $node, PhpWriter $writer, ?
};

return $writer->write(
'$this->addBlock(%word, %var, [[$this, %var]], %var);',
'$this->addBlock($__nm = %word, %var, [[$this, %var]], %var);',
$data->name,
implode($node->context),
$func,
Expand Down Expand Up @@ -449,18 +448,17 @@ private function beginDynamicSnippet(MacroNode $node, PhpWriter $writer): string
};
}
$node->attrCode = $writer->write(
"<?php echo ' {$this->snippetAttribute}=\"' . htmlspecialchars(\$this->global->snippetDriver->getHtmlId(%word)) . '\"' ?>",
"<?php echo ' {$this->snippetAttribute}=\"' . htmlspecialchars(\$this->global->snippetDriver->getHtmlId(\$__nm = %word)) . '\"' ?>",
$data->name
);
return $writer->write('$this->global->snippetDriver->enter(%word, %var); try {', $data->name, SnippetDriver::TYPE_DYNAMIC);
return $writer->write('$this->global->snippetDriver->enter($__nm, %var); try {', SnippetDriver::TYPE_DYNAMIC);
}

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

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

private function isDynamic(string $name): bool
{
return strpos($name, '$') !== false;
return strpos($name, '$') !== false || strpos($name, ' ') !== false;
}
}
1 change: 1 addition & 0 deletions tests/Latte/BlockMacros.dynamicblock.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ $template = <<<'EOD'
{block "strip$name"|striptags}<span>hello</span>{/block}
{block rand() < 5 ? a : b} expression {/block}
EOD;

Assert::matchFile(
Expand Down
1 change: 1 addition & 0 deletions tests/Latte/BlockMacros.ifset.block.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Assert::same('<?php if ($this->hasBlock("block")) { ?>', $compiler->expandMacro(
Assert::same('<?php if ($this->hasBlock($foo)) { ?>', $compiler->expandMacro('ifset', '#$foo')->openingCode);
Assert::same('<?php if ($this->hasBlock("foo")) { ?>', $compiler->expandMacro('ifset', 'block foo')->openingCode);
Assert::same('<?php if ($this->hasBlock($foo)) { ?>', $compiler->expandMacro('ifset', 'block $foo')->openingCode);
Assert::same('<?php if ($this->hasBlock(("f" . "oo"))) { ?>', $compiler->expandMacro('ifset', 'block "f" . "oo"')->openingCode);
Assert::same(
'<?php if ($this->hasBlock("foo") && $this->hasBlock("block") && isset($item)) { ?>',
$compiler->expandMacro('ifset', 'block foo, block, $item')->openingCode
Expand Down
9 changes: 9 additions & 0 deletions tests/Latte/BlockMacros.import.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ $latte->setLoader(new Latte\Loaders\StringLoader([
{import "inc"}
{include test}
',
'main-dynamic' => '
{import "i" . "nc"}
{include test}
',
'inc' => '
outer text
{define test}
Expand All @@ -34,3 +38,8 @@ Assert::match(
'Test block',
trim($latte->renderToString('main'))
);

Assert::match(
'Test block',
trim($latte->renderToString('main-dynamic'))
);
6 changes: 6 additions & 0 deletions tests/Latte/BlockMacros.include.block.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $latte->setLoader(new Latte\Loaders\StringLoader([
'main7' => '{define block}[block {$var}]{/define} {var $name = block} before {include block $name, var => 1} after',

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

Assert::match(
Expand Down Expand Up @@ -63,3 +64,8 @@ Assert::match(
' before block 1 after',
$latte->renderToString('main8')
);

Assert::match(
' before block 2 after',
$latte->renderToString('main9')
);
16 changes: 16 additions & 0 deletions tests/Latte/BlockMacros.snippet.dynamic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ Assert::matchFile(
__DIR__ . '/expected/BlockMacros.snippet.dynamic.phtml',
$latte->compile($template)
);


$template = <<<'EOD'
{snippet outer}
{foreach array(1,2,3) as $id}
{snippet 'inner-' . $id}
#{$id}
{/snippet}
{/foreach}
{/snippet outer}
EOD;

Assert::matchFile(
__DIR__ . '/expected/BlockMacros.snippet.dynamic2.phtml',
$latte->compile($template)
);
10 changes: 5 additions & 5 deletions tests/Latte/expected/BlockMacros.define.args1.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ final class Template%a% extends Latte\Runtime\Template
}
echo '
a) ';
$this->renderBlock('test', [1] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'test', [1] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo '
b) ';
$this->renderBlock('outer', ($this->hasBlock('outer', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'outer', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo '
';
$var1 = 'outer';
echo 'c) ';
$this->renderBlock('test', ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'test', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo '
d) ';
$this->renderBlock('test', [null] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'test', [null] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
return get_defined_vars();
}

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

}

Expand Down
4 changes: 2 additions & 2 deletions tests/Latte/expected/BlockMacros.define.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ final class Template%a% extends Latte\Runtime\Template
return get_defined_vars();
}
echo "\n";
$this->renderBlock('test', ['var' => 20] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'test', ['var' => 20] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->renderBlock('true', ($this->hasBlock('true', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'true', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
return get_defined_vars();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/expected/BlockMacros.define.typehints.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Template%a% extends Latte\Runtime\Template
return get_defined_vars();
}
echo "\n";
$this->renderBlock('test', [1] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'test', [1] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
return get_defined_vars();
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Latte/expected/BlockMacros.dynamicblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@


hello

expression
31 changes: 21 additions & 10 deletions tests/Latte/expected/BlockMacros.dynamicblock.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,34 @@ final class Template%a% extends Latte\Runtime\Template
';
$iterations = 0;
foreach ($iterator = $__it = new LR\CachingIterator(['dynamic', 'static'], $__it ?? null) as $name) {
$this->addBlock($name, 'html', [[$this, 'blockName']], null);
$this->renderBlock($name, get_defined_vars());
$this->addBlock($__nm = $name, 'html', [[$this, 'blockName']], null);
$this->renderBlock($__nm, get_defined_vars());
echo "\n";
$iterations++;
}
$iterator = $__it = $__it->getParent();
echo "\n";
$this->renderBlock('dynamic', ['var' => 20] + ($this->hasBlock('dynamic', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'dynamic', ['var' => 20] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->renderBlock('static', ['var' => 30] + ($this->hasBlock('static', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'static', ['var' => 30] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->renderBlock(($name . ''), ['var' => 40] + ($this->hasBlock(($name . ''), true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = ($name . ''), ['var' => 40] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->addBlock("word$name", 'html', [[$this, 'blockWord_name']], null);
$this->renderBlock("word$name", get_defined_vars());
$this->addBlock($__nm = "word$name", 'html', [[$this, 'blockWord_name']], null);
$this->renderBlock($__nm, get_defined_vars());
echo '
';
$this->addBlock("strip$name", 'html', [[$this, 'blockStrip_name']], null);
$this->renderBlock("strip$name", get_defined_vars(), function ($s, $type) {
$this->addBlock($__nm = "strip$name", 'html', [[$this, 'blockStrip_name']], null);
$this->renderBlock($__nm, get_defined_vars(), function ($s, $type) {
$__fi = new LR\FilterInfo($type);
return LR\Filters::convertTo($__fi, 'html', $this->filters->filterContent('striptags', $__fi, $s));
});
echo "\n";
echo '
';
$this->addBlock($__nm = (rand() < 5 ? 'a' : 'b'), 'html', [[$this, 'blockRand_5_a_b']], null);
$this->renderBlock($__nm, get_defined_vars());
return get_defined_vars();
}

Expand Down Expand Up @@ -88,6 +92,13 @@ final class Template%a% extends Latte\Runtime\Template
}


public function blockRand_5_a_b(array $__args): void
{
extract($__args);
echo ' expression';
}


public function blockStatic(array $__args): void
{
extract($__args);
Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/expected/BlockMacros.import.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class Template%a% extends Latte\Runtime\Template
public function main(): array
{
%A%
$this->renderBlock('test', ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'test', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo ' ';
%A%
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/expected/BlockMacros.includeblock.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class Template%a% extends Latte\Runtime\Template
$this->createTemplate("inc", get_defined_vars(), "includeblock")->renderToContentType('html');
echo rtrim(ob_get_clean());
echo "\n";
$this->renderBlock('test', ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'test', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
%A%
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ final class Template%a% extends Latte\Runtime\Template
<div id="content">
';
$this->renderBlock('content', ($this->hasBlock('content', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'content', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->renderBlock('content', ($this->hasBlock('content', true) ? get_defined_vars() : $this->params), function ($s, $type) {
$this->renderBlock($__nm = 'content', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), function ($s, $type) {
$__fi = new LR\FilterInfo($type);
return LR\Filters::convertTo($__fi, 'html', $this->filters->filterContent('upper', $__fi, $this->filters->filterContent('striphtml', $__fi, $s)));
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Latte/expected/BlockMacros.inheritance.child1.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ final class Template%a% extends Latte\Runtime\Template
{
extract($__args);
echo 'Homepage | ';
$this->renderBlockParent('title', ($this->hasBlock('title', true) ? get_defined_vars() : $this->params));
$this->renderBlockParent('title', ($this->hasBlock('title', true) ? get_defined_vars() : $this->params));
$this->renderBlockParent($__nm = 'title', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params));
$this->renderBlockParent($__nm = 'title', ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params));

}

Expand Down
18 changes: 9 additions & 9 deletions tests/Latte/expected/BlockMacros.local.dynamic.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ final class Template%a% extends Latte\Runtime\Template
';
$iterations = 0;
foreach ($iterator = $__it = new LR\CachingIterator(['dynamic', 'static'], $__it ?? null) as $name) {
$this->addBlock($name, 'html', [[$this, 'blockName']], 'local');
$this->renderBlock($name, get_defined_vars());
$this->addBlock($__nm = $name, 'html', [[$this, 'blockName']], 'local');
$this->renderBlock($__nm, get_defined_vars());
echo "\n";
$iterations++;
}
$iterator = $__it = $__it->getParent();
echo "\n";
$this->renderBlock('dynamic', ['var' => 20] + ($this->hasBlock('dynamic', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'dynamic', ['var' => 20] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->renderBlock('static', ['var' => 30] + ($this->hasBlock('static', true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = 'static', ['var' => 30] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->renderBlock(($name . ''), ['var' => 40] + ($this->hasBlock(($name . ''), true) ? get_defined_vars() : $this->params), 'html');
$this->renderBlock($__nm = ($name . ''), ['var' => 40] + ($this->hasBlock($__nm, true) ? get_defined_vars() : $this->params), 'html');
echo "\n";
$this->addBlock("word$name", 'html', [[$this, 'blockWord_name']], 'local');
$this->renderBlock("word$name", get_defined_vars());
$this->addBlock($__nm = "word$name", 'html', [[$this, 'blockWord_name']], 'local');
$this->renderBlock($__nm, get_defined_vars());
echo '
';
$this->addBlock("strip$name", 'html', [[$this, 'blockStrip_name']], 'local');
$this->renderBlock("strip$name", get_defined_vars(), function ($s, $type) {
$this->addBlock($__nm = "strip$name", 'html', [[$this, 'blockStrip_name']], 'local');
$this->renderBlock($__nm, get_defined_vars(), function ($s, $type) {
$__fi = new LR\FilterInfo($type);
return LR\Filters::convertTo($__fi, 'html', $this->filters->filterContent('striptags', $__fi, $s));
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Latte/expected/BlockMacros.snippet.block.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ final class Template%a% extends Latte\Runtime\Template
{
extract($__args);
echo '<div';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId("inner-$id")) . '"';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($__nm = "inner-$id")) . '"';
echo '>
';
$this->global->snippetDriver->enter("inner-$id", 'dynamic');
$this->global->snippetDriver->enter($__nm, 'dynamic');
try {
echo ' dynamic
';
Expand Down
8 changes: 4 additions & 4 deletions tests/Latte/expected/BlockMacros.snippet.dynamic.alt.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ final class Template%a% extends Latte\Runtime\Template
$iterations = 0;
foreach (array(1,2,3) as $id) {
echo ' <div';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId("inner-$id")) . '"';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($__nm = "inner-$id")) . '"';
echo '>
';
$this->global->snippetDriver->enter("inner-$id", 'dynamic');
$this->global->snippetDriver->enter($__nm, 'dynamic');
try {
echo ' #';
echo LR\Filters::escapeHtmlText($id) /* line 4 */;
Expand Down Expand Up @@ -72,10 +72,10 @@ final class Template%a% extends Latte\Runtime\Template
$iterations = 0;
foreach (array(1,2,3) as $id) {
echo ' <div';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId("inner-$id")) . '"';
echo ' id="' . htmlspecialchars($this->global->snippetDriver->getHtmlId($__nm = "inner-$id")) . '"';
echo '>
';
$this->global->snippetDriver->enter("inner-$id", 'dynamic');
$this->global->snippetDriver->enter($__nm, 'dynamic');
try {
echo ' #';
echo LR\Filters::escapeHtmlText($id) /* line 13 */;
Expand Down
4 changes: 2 additions & 2 deletions tests/Latte/expected/BlockMacros.snippet.dynamic.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ final class Template%a% extends Latte\Runtime\Template
$iterations = 0;
foreach (array(1,2,3) as $id) {
echo ' <div id="';
echo htmlspecialchars($this->global->snippetDriver->getHtmlId("inner-$id"));
echo htmlspecialchars($this->global->snippetDriver->getHtmlId($__nm = "inner-$id"));
echo '">';
$this->global->snippetDriver->enter("inner-$id", 'dynamic');
$this->global->snippetDriver->enter($__nm, 'dynamic');
try {
echo '
#';
Expand Down
Loading

0 comments on commit f88777b

Please sign in to comment.