Skip to content

Commit

Permalink
anonymous {block} create variable scope (possible BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 26, 2021
1 parent 268a54d commit 7986fa8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
20 changes: 11 additions & 9 deletions src/Latte/Macros/BlockMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function macroExtends(MacroNode $node, PhpWriter $writer): void
/**
* {block [local] [name]}
*/
public function macroBlock(MacroNode $node, PhpWriter $writer): string
public function macroBlock(MacroNode $node, PhpWriter $writer)
{
[$name, $local] = $node->tokenizer->fetchWordWithModifier('local');
$layer = $local ? Template::LAYER_LOCAL : null;
Expand All @@ -258,15 +258,17 @@ public function macroBlock(MacroNode $node, PhpWriter $writer): string
$this->checkExtraArgs($node);

if ($data->name === '') {
if ($node->modifiers === '') {
return '';
$node->openingCode = '<?php (function () { extract(func_get_arg(0)); ?>';
$node->closingCode = '<?php })(get_defined_vars()); ?>';
if ($node->modifiers !== '') {
$node->modifiers .= '|escape';
$node->openingCode = $writer->write('<?php ob_start(function () {}) %node.line; ?>') . $node->openingCode;
$node->closingCode .= $writer->write(
'<?php $ʟ_fi = new LR\FilterInfo(%var); echo %modifyContent(ob_get_clean()); ?>',
implode($node->context)
);
}
$node->modifiers .= '|escape';
$node->closingCode = $writer->write(
'<?php $ʟ_fi = new LR\FilterInfo(%var); echo %modifyContent(ob_get_clean()); ?>',
implode($node->context)
);
return $writer->write('ob_start(function () {}) %node.line;');
return;
}

if (Helpers::startsWith((string) $node->context[1], Latte\Compiler::CONTEXT_HTML_ATTRIBUTE)) {
Expand Down
7 changes: 3 additions & 4 deletions tests/Latte/expected/BlockMacros.vars.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@

a

blockmod

a

blockmod

a

block
a
25 changes: 15 additions & 10 deletions tests/Latte/expected/BlockMacros.vars.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
';
ob_start(function () {}) /* line %d% */;
echo ' ';
echo LR\Filters::escapeHtmlText($var) /* line %d% */;
echo "\n";
$var = 'blockmod' /* line %d% */;
(function () {
extract(func_get_arg(0));
echo ' ';
echo LR\Filters::escapeHtmlText($var) /* line %d% */;
echo "\n";
$var = 'blockmod' /* line %d% */;
})(get_defined_vars());
$ʟ_fi = new LR\FilterInfo('html');
echo LR\Filters::convertTo($ʟ_fi, 'html', $this->filters->filterContent('trim', $ʟ_fi, ob_get_clean()));
echo '
Expand All @@ -34,13 +37,15 @@
echo '
';
echo LR\Filters::escapeHtmlText($var) /* line %d% */;
echo "\n";
$var = 'block' /* line %d% */;
echo '
';
(function () {
extract(func_get_arg(0));
echo ' ';
echo LR\Filters::escapeHtmlText($var) /* line %d% */;
echo "\n";
$var = 'block' /* line %d% */;
})(get_defined_vars());
echo "\n";
echo LR\Filters::escapeHtmlText($var) /* line %d% */;
return get_defined_vars();
}
Expand Down

0 comments on commit 7986fa8

Please sign in to comment.