Skip to content

Commit

Permalink
UIMacros: source of parent name is $parentName property instead of ge…
Browse files Browse the repository at this point in the history
…tParentName()
  • Loading branch information
dg committed May 5, 2016
1 parent 91fa976 commit 8eb9618
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
18 changes: 12 additions & 6 deletions src/Bridges/ApplicationLatte/UIMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
class UIMacros extends Latte\Macros\MacroSet
{
/** @var bool */
private $extends;


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


Expand All @@ -60,7 +60,12 @@ public function initialize()
*/
public function finalize()
{
return ['if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueue)) return; $template = $this->params["template"];', ''];
return [
'if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueue)) return; $template = $this->params["template"];',
'',
$this->extends ? '' : '$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
? $this->params["_control"]->findLayoutTemplateFile() : NULL);',
];
}


Expand Down Expand Up @@ -123,10 +128,11 @@ public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
*/
public function macroExtends(MacroNode $node, PhpWriter $writer)
{
$this->extends = TRUE;
if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
return FALSE;
}
$this->getCompiler()->addMethod('getParentName', 'return $this->params["_presenter"]->findLayoutTemplateFile();');
return $writer->write('$this->parentName = $this->params["_presenter"]->findLayoutTemplateFile();');
}


Expand Down
8 changes: 8 additions & 0 deletions tests/Bridges.Latte/Template.getParentName().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,54 @@ $template = $latte->createTemplate(
'',
['_control' => new MockPresenter]
);
$template->prepare();
Assert::null($template->getParentName());

$template = $latte->createTemplate(
'{block}...{/block}',
['_control' => new MockPresenter]
);
$template->prepare();
Assert::null($template->getParentName());

$template = $latte->createTemplate(
'{block name}...{/block}',
['_control' => new MockPresenter]
);
$template->prepare();
Assert::same('layout.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends "file.latte"} {block name}...{/block}',
['_control' => new MockPresenter]
);
$template->prepare();
Assert::same('file.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends "file.latte"}',
['_control' => new MockPresenter]
);
$template->prepare();
Assert::same('file.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends $file} {block name}...{/block}',
['_control' => new MockPresenter, 'file' => 'file.latte']
);
$template->prepare();
Assert::same('file.latte', $template->getParentName());

$template = $latte->createTemplate(
'{extends none}',
['_control' => new MockPresenter]
);
$template->prepare();
Assert::null($template->getParentName());

$template = $latte->createTemplate(
'{extends auto}',
['_presenter' => new MockPresenter]
);
$template->prepare();
Assert::same('layout.latte', $template->getParentName());
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ class Template%a% extends Latte\Template
}


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


Expand Down
7 changes: 5 additions & 2 deletions tests/Bridges.Latte/expected/UIMacros.dynamicsnippets.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ class Template%a% extends Latte\Template
}


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


Expand Down
7 changes: 5 additions & 2 deletions tests/Bridges.Latte/expected/UIMacros.snippet.alt.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ class Template%a% extends Latte\Template
}


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


Expand Down
7 changes: 5 additions & 2 deletions tests/Bridges.Latte/expected/UIMacros.snippet.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ class Template%a% extends Latte\Template
}


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


Expand Down

0 comments on commit 8eb9618

Please sign in to comment.