Skip to content

Commit

Permalink
BlockMacros: fixed variables visibility between blocks depend on bloc…
Browse files Browse the repository at this point in the history
…k order [Closes #178]
  • Loading branch information
dg committed Nov 15, 2020
1 parent e388bae commit 37f40a4
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 26 deletions.
10 changes: 6 additions & 4 deletions src/Latte/Macros/BlockMacros.php
Expand Up @@ -136,13 +136,15 @@ public function macroInclude(MacroNode $node, PhpWriter $writer)
$node->modifiers .= '|escape';
}

$phpName = strpos($name, '$') === false
? PhpHelpers::dump($name)
: $writer->formatWord($name);

return $writer->write(
'$this->renderBlock' . ($parent ? 'Parent' : '') . '('
. (strpos($name, '$') === false ? PhpHelpers::dump($name) : $writer->formatWord($name))
. $phpName
. ', %node.array? + '
. (isset($this->blocks[$this->index][$name]) || isset($this->blocks[Template::LAYER_LOCAL][$name])
? 'get_defined_vars()'
: '$this->params')
. '($this->hasBlock(' . $phpName . ', 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
5 changes: 3 additions & 2 deletions src/Latte/Runtime/Template.php
Expand Up @@ -381,9 +381,10 @@ protected function leaveBlockLayer($id): void
}


public function hasBlock(string $name): bool
public function hasBlock(string $name, bool $definedInTemplate = false): bool
{
return isset($this->blocks[self::LAYER_LOCAL][$name]) || isset($this->blocks[$this->index][$name]);
$src = $definedInTemplate ? static::BLOCKS : $this->blocks;
return isset($src[$this->index][$name]) || isset($src[self::LAYER_LOCAL][$name]);
}


Expand Down
40 changes: 40 additions & 0 deletions tests/Latte/BlockMacros.block6.phpt
@@ -0,0 +1,40 @@
<?php

/**
* Test: Latte\Engine and blocks.
*/

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);



Assert::match(
'
X
',
$latte->renderToString(
'
{define a}
{var $x = "X"}
{include #b}
{/define}
{define b}
{$x}
{/define}
{include a}
'
)
);
10 changes: 10 additions & 0 deletions tests/Latte/BlockMacros.local.phpt
Expand Up @@ -53,6 +53,16 @@ Assert::same(
);


Assert::same(
'5',
trim($latte->renderToString('
{var $i = 5}
{include abc}
{define local abc}{$i}{/define}
'))
);


Assert::same(
'5',
trim($latte->renderToString('
Expand Down
12 changes: 6 additions & 6 deletions tests/Latte/expected/BlockMacros.defineblock.phtml
Expand Up @@ -24,31 +24,31 @@ final class Template%a% extends Latte\Runtime\Template
?>

a) <?php
$this->renderBlock('test', ['var' => 20] + get_defined_vars(), 'html');
$this->renderBlock('test', ['var' => 20] + ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
?>



b) <?php
$this->renderBlock('testargs', [1] + get_defined_vars(), 'html');
$this->renderBlock('testargs', [1] + ($this->hasBlock('testargs', true) ? get_defined_vars() : $this->params), 'html');
?>



g) <?php
$this->renderBlock('outer', get_defined_vars(), 'html');
$this->renderBlock('outer', ($this->hasBlock('outer', true) ? get_defined_vars() : $this->params), 'html');
?>


<?php
$var1 = 'outer';
?>h) <?php
$this->renderBlock('testargs', get_defined_vars(), 'html');
$this->renderBlock('testargs', ($this->hasBlock('testargs', true) ? get_defined_vars() : $this->params), 'html');
?>


i) <?php
$this->renderBlock('testargs', [null] + get_defined_vars(), 'html');
$this->renderBlock('testargs', [null] + ($this->hasBlock('testargs', true) ? get_defined_vars() : $this->params), 'html');
return get_defined_vars();
}

Expand Down Expand Up @@ -76,7 +76,7 @@ i) <?php
public function blockOuter(array $__args): void
{
extract($__args);
$this->renderBlock('testargs', ['hello'] + get_defined_vars(), 'html');
$this->renderBlock('testargs', ['hello'] + ($this->hasBlock('testargs', true) ? get_defined_vars() : $this->params), 'html');

}

Expand Down
6 changes: 3 additions & 3 deletions tests/Latte/expected/BlockMacros.dynamicblock.phtml
Expand Up @@ -36,15 +36,15 @@ final class Template%a% extends Latte\Runtime\Template
?>

<?php
$this->renderBlock('dynamic', ['var' => 20] + $this->params, 'html');
$this->renderBlock('dynamic', ['var' => 20] + ($this->hasBlock('dynamic', true) ? get_defined_vars() : $this->params), 'html');
?>

<?php
$this->renderBlock('static', ['var' => 30] + get_defined_vars(), 'html');
$this->renderBlock('static', ['var' => 30] + ($this->hasBlock('static', true) ? get_defined_vars() : $this->params), 'html');
?>

<?php
$this->renderBlock($name . '', ['var' => 40] + $this->params, 'html');
$this->renderBlock($name . '', ['var' => 40] + ($this->hasBlock($name . '', true) ? get_defined_vars() : $this->params), 'html');
?>

<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/expected/BlockMacros.import.phtml
Expand Up @@ -7,7 +7,7 @@ final class Template%a% extends Latte\Runtime\Template
public function main(): array
{
%A%
$this->renderBlock('test', $this->params, 'html');
$this->renderBlock('test', ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
?> <?php
%A%
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/expected/BlockMacros.includeblock.phtml
Expand Up @@ -13,7 +13,7 @@ final class Template%a% extends Latte\Runtime\Template
?>

<?php
$this->renderBlock('test', $this->params, 'html');
$this->renderBlock('test', ($this->hasBlock('test', true) ? get_defined_vars() : $this->params), 'html');
?> <?php
%A%
}
Expand Down
Expand Up @@ -34,11 +34,11 @@ final class Template%a% extends Latte\Runtime\Template

<div id="content">
<?php
$this->renderBlock('content', $this->params, 'html');
$this->renderBlock('content', ($this->hasBlock('content', true) ? get_defined_vars() : $this->params), 'html');
?>

<?php
$this->renderBlock('content', $this->params, function ($s, $type) {
$this->renderBlock('content', ($this->hasBlock('content', 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
Expand Up @@ -48,8 +48,8 @@ final class Template%a% extends Latte\Runtime\Template
{
extract($__args);
?>Homepage | <?php
$this->renderBlockParent('title', get_defined_vars());
$this->renderBlockParent('title', get_defined_vars());
$this->renderBlockParent('title', ($this->hasBlock('title', true) ? get_defined_vars() : $this->params));
$this->renderBlockParent('title', ($this->hasBlock('title', true) ? get_defined_vars() : $this->params));

}

Expand Down
6 changes: 3 additions & 3 deletions tests/Latte/expected/BlockMacros.local.dynamic.phtml
Expand Up @@ -36,15 +36,15 @@ final class Template%a% extends Latte\Runtime\Template
?>

<?php
$this->renderBlock('dynamic', ['var' => 20] + $this->params, 'html');
$this->renderBlock('dynamic', ['var' => 20] + ($this->hasBlock('dynamic', true) ? get_defined_vars() : $this->params), 'html');
?>

<?php
$this->renderBlock('static', ['var' => 30] + get_defined_vars(), 'html');
$this->renderBlock('static', ['var' => 30] + ($this->hasBlock('static', true) ? get_defined_vars() : $this->params), 'html');
?>

<?php
$this->renderBlock($name . '', ['var' => 40] + $this->params, 'html');
$this->renderBlock($name . '', ['var' => 40] + ($this->hasBlock($name . '', true) ? get_defined_vars() : $this->params), 'html');
?>

<?php
Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/expected/macros.general.html.phtml
Expand Up @@ -272,7 +272,7 @@ var prop2 = <?php echo LR\Filters::escapeJs($people) /* line 82 */ ?>;
?> <li><?php echo LR\Filters::escapeHtmlText($counter++) /* line 105 */ ?> <?php
if (is_array($item)) {
?> <?php
$this->renderBlock('menu', ['menu' => $item] + get_defined_vars(), 'html');
$this->renderBlock('menu', ['menu' => $item] + ($this->hasBlock('menu', true) ? get_defined_vars() : $this->params), 'html');
?> <?php
}
else {
Expand Down
2 changes: 1 addition & 1 deletion tests/Latte/expected/macros.general.xhtml.phtml
Expand Up @@ -274,7 +274,7 @@ var prop2 = <?php echo LR\Filters::escapeJs($people) /* line 82 */ ?>;
?> <li><?php echo LR\Filters::escapeHtmlText($counter++) /* line 105 */ ?> <?php
if (is_array($item)) {
?> <?php
$this->renderBlock('menu', ['menu' => $item] + get_defined_vars(), 'xhtml');
$this->renderBlock('menu', ['menu' => $item] + ($this->hasBlock('menu', true) ? get_defined_vars() : $this->params), 'xhtml');
?> <?php
}
else {
Expand Down

0 comments on commit 37f40a4

Please sign in to comment.