Skip to content

Commit

Permalink
UIMacros: looking for layout template moved to UIMacros
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 29, 2016
1 parent 5dbd819 commit ed03dd1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function createTemplate(UI\Control $control = NULL)
array_unshift($latte->onCompile, function ($latte) use ($control, $template) {
$latte->getParser()->shortNoEscape = TRUE;
$latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler()));
UIMacros::install($latte->getCompiler());
UIMacros::install($latte->getCompiler(), $control instanceof UI\Presenter ? $control : NULL);
if (class_exists(Nette\Bridges\FormsLatte\FormMacros::class)) {
Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler());
}
Expand Down
24 changes: 17 additions & 7 deletions src/Bridges/ApplicationLatte/UIMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
namespace Nette\Bridges\ApplicationLatte;

use Nette;
use Nette\Application\UI\Presenter;
use Nette\Utils\Strings;
use Latte;
use Latte\MacroNode;
use Latte\PhpWriter;
use Latte\CompileException;
use Nette\Utils\Strings;


/**
Expand All @@ -24,10 +25,15 @@
*/
class UIMacros extends Latte\Macros\MacroSet
{
/** @var Presenter|NULL */
private $presenter;

public static function install(Latte\Compiler $compiler)

public static function install(Latte\Compiler $compiler, Presenter $presenter = NULL)
{
$me = new static($compiler);
$me->presenter = $presenter;

$me->addMacro('control', [$me, 'macroControl']);

$me->addMacro('href', NULL, NULL, function (MacroNode $node, PhpWriter $writer) use ($me) {
Expand All @@ -47,10 +53,11 @@ public static function install(Latte\Compiler $compiler)
*/
public function initialize()
{
$this->getCompiler()->addMethod('getExtends', '
return !$this->getParent() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
? $this->params["_control"]->findLayoutTemplateFile() : NULL;
');
$layout = $this->presenter
? '!$this->getParent() ? ' . var_export($this->presenter->findLayoutTemplateFile(), TRUE) . ' : NULL'
: '!$this->getParent() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
? $this->params["_control"]->findLayoutTemplateFile() : NULL';
$this->getCompiler()->addMethod('getExtends', "return $layout;");
}


Expand Down Expand Up @@ -126,7 +133,10 @@ public function macroExtends(MacroNode $node, PhpWriter $writer)
if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
return FALSE;
}
$this->getCompiler()->addMethod('getExtends', 'return $this->params["_presenter"]->findLayoutTemplateFile();');
$layout = $this->presenter
? var_export($this->presenter->findLayoutTemplateFile(), TRUE)
: '$this->params["_presenter"]->findLayoutTemplateFile()';
$this->getCompiler()->addMethod('getExtends', "return $layout;");
}


Expand Down

0 comments on commit ed03dd1

Please sign in to comment.