Skip to content

Commit 5d63a8d

Browse files
committed
UIMacros: source of parent name is $parentName property instead of getParentName()
1 parent 91fa976 commit 5d63a8d

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
class UIMacros extends Latte\Macros\MacroSet
2626
{
27+
/** @var bool */
28+
private $extends;
29+
2730

2831
public static function install(Latte\Compiler $compiler)
2932
{
@@ -47,10 +50,7 @@ public static function install(Latte\Compiler $compiler)
4750
*/
4851
public function initialize()
4952
{
50-
$this->getCompiler()->addMethod('getParentName', '
51-
return $this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
52-
? $this->params["_control"]->findLayoutTemplateFile() : NULL;
53-
');
53+
$this->extends = FALSE;
5454
}
5555

5656

@@ -60,7 +60,12 @@ public function initialize()
6060
*/
6161
public function finalize()
6262
{
63-
return ['if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueue)) return; $template = $this->params["template"];', ''];
63+
return [
64+
'if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueue)) return; $template = $this->params["template"];',
65+
'',
66+
$this->extends ? '' : '$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
67+
? $this->params["_control"]->findLayoutTemplateFile() : NULL);',
68+
];
6469
}
6570

6671

@@ -123,10 +128,11 @@ public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
123128
*/
124129
public function macroExtends(MacroNode $node, PhpWriter $writer)
125130
{
131+
$this->extends = TRUE;
126132
if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
127133
return FALSE;
128134
}
129-
$this->getCompiler()->addMethod('getParentName', 'return $this->params["_presenter"]->findLayoutTemplateFile();');
135+
return $writer->write('$this->parentName = $this->params["_presenter"]->findLayoutTemplateFile();');
130136
}
131137

132138

tests/Bridges.Latte/Template.getParentName().phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,54 @@ $template = $latte->createTemplate(
2424
'',
2525
['_control' => new MockPresenter]
2626
);
27+
$template->prepare();
2728
Assert::null($template->getParentName());
2829

2930
$template = $latte->createTemplate(
3031
'{block}...{/block}',
3132
['_control' => new MockPresenter]
3233
);
34+
$template->prepare();
3335
Assert::null($template->getParentName());
3436

3537
$template = $latte->createTemplate(
3638
'{block name}...{/block}',
3739
['_control' => new MockPresenter]
3840
);
41+
$template->prepare();
3942
Assert::same('layout.latte', $template->getParentName());
4043

4144
$template = $latte->createTemplate(
4245
'{extends "file.latte"} {block name}...{/block}',
4346
['_control' => new MockPresenter]
4447
);
48+
$template->prepare();
4549
Assert::same('file.latte', $template->getParentName());
4650

4751
$template = $latte->createTemplate(
4852
'{extends "file.latte"}',
4953
['_control' => new MockPresenter]
5054
);
55+
$template->prepare();
5156
Assert::same('file.latte', $template->getParentName());
5257

5358
$template = $latte->createTemplate(
5459
'{extends $file} {block name}...{/block}',
5560
['_control' => new MockPresenter, 'file' => 'file.latte']
5661
);
62+
$template->prepare();
5763
Assert::same('file.latte', $template->getParentName());
5864

5965
$template = $latte->createTemplate(
6066
'{extends none}',
6167
['_control' => new MockPresenter]
6268
);
69+
$template->prepare();
6370
Assert::null($template->getParentName());
6471

6572
$template = $latte->createTemplate(
6673
'{extends auto}',
6774
['_presenter' => new MockPresenter]
6875
);
76+
$template->prepare();
6977
Assert::same('layout.latte', $template->getParentName());

tests/Bridges.Latte/expected/UIMacros.dynamicsnippets.alt.phtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ class Template%a% extends Latte\Template
2929
}
3030

3131

32-
function getParentName()
32+
function prepare()
3333
{
34-
%A%
34+
extract($this->params);
35+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
36+
? $this->params["_control"]->findLayoutTemplateFile() : NULL);
37+
return get_defined_vars();
3538
}
3639

3740

tests/Bridges.Latte/expected/UIMacros.dynamicsnippets.phtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ class Template%a% extends Latte\Template
2525
}
2626

2727

28-
function getParentName()
28+
function prepare()
2929
{
30-
%A%
30+
extract($this->params);
31+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
32+
? $this->params["_control"]->findLayoutTemplateFile() : NULL);
33+
return get_defined_vars();
3134
}
3235

3336

tests/Bridges.Latte/expected/UIMacros.snippet.alt.phtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ class Template%a% extends Latte\Template
4141
}
4242

4343

44-
function getParentName()
44+
function prepare()
4545
{
46-
%A%
46+
extract($this->params);
47+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
48+
? $this->params["_control"]->findLayoutTemplateFile() : NULL);
49+
return get_defined_vars();
4750
}
4851

4952

tests/Bridges.Latte/expected/UIMacros.snippet.phtml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ class Template%a% extends Latte\Template
4747
}
4848

4949

50-
function getParentName()
50+
function prepare()
5151
{
52-
%A%
52+
extract($this->params);
53+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
54+
? $this->params["_control"]->findLayoutTemplateFile() : NULL);
55+
return get_defined_vars();
5356
}
5457

5558

0 commit comments

Comments
 (0)