Skip to content

Commit

Permalink
Latte: {layout auto} always call Presenter::findLayoutTemplateFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 9, 2023
1 parent 32db4c2 commit 0729ede
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Bridges/ApplicationLatte/UIExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
namespace Nette\Bridges\ApplicationLatte;

use Latte;
use Latte\Compiler\Nodes\Php\Expression\AuxiliaryNode;
use Latte\Compiler\Nodes\TemplateNode;
use Latte\Compiler\Tag;
use Latte\Essential\Nodes\ExtendsNode;
use Nette;
use Nette\Application\UI;

Expand Down Expand Up @@ -64,6 +67,8 @@ public function getTags(): array
'templatePrint' => [Nodes\TemplatePrintNode::class, 'create'],
'snippet' => [Nodes\SnippetNode::class, 'create'],
'snippetArea' => [Nodes\SnippetAreaNode::class, 'create'],
'layout' => [$this, 'createExtendsNode'],
'extends' => [$this, 'createExtendsNode'],
];
}

Expand Down Expand Up @@ -106,4 +111,15 @@ private function findNonce(Nette\Http\IResponse $httpResponse): ?string
?: $httpResponse->getHeader('Content-Security-Policy-Report-Only');
return preg_match('#\s\'nonce-([\w+/]+=*)\'#', (string) $header, $m) ? $m[1] : null;
}


public static function createExtendsNode(Tag $tag): ExtendsNode
{
$auto = $tag->parser->stream->is('auto');
$node = ExtendsNode::create($tag);
if ($auto) {
$node->extends = new AuxiliaryNode(fn() => '$this->global->uiPresenter->findLayoutTemplateFile()');
}
return $node;
}
}
8 changes: 8 additions & 0 deletions tests/Bridges.Latte3/Template.getParentName().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ Assert::exception(
LogicException::class, // missing template
);
Assert::same('layout.latte', $template->getParentName());


$latte->setLoader(new Latte\Loaders\StringLoader([
'main.latte' => '{extends foo.latte}',
'foo.latte' => '{extends auto}',
'layout.latte' => 'layout',
]));
Assert::same('layout', $template = $latte->renderToString('main.latte'));

0 comments on commit 0729ede

Please sign in to comment.