From ba8a32e5b2fe0bacd839be9323112c54228e33a0 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 4 Jan 2020 23:53:37 +0100 Subject: [PATCH] Control, Presenter::createTemplate() tries to create custom Template according to naming convention --- src/Application/UI/Control.php | 11 ++++++++++- src/Application/UI/Presenter.php | 2 +- src/Bridges/ApplicationLatte/TemplateFactory.php | 9 +++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Application/UI/Control.php b/src/Application/UI/Control.php index da20357ee..fa9e15ffa 100644 --- a/src/Application/UI/Control.php +++ b/src/Application/UI/Control.php @@ -54,7 +54,16 @@ final public function getTemplate(): ITemplate protected function createTemplate(): ITemplate { $templateFactory = $this->templateFactory ?: $this->getPresenter()->getTemplateFactory(); - return $templateFactory->createTemplate($this); + return $templateFactory->createTemplate($this, self::formatTemplateClass()); + } + + + public static function formatTemplateClass(): ?string + { + $class = preg_replace('#Presenter$|Control$#', 'Template', static::class); + return is_a($class, Nette\Bridges\ApplicationLatte\LatteTemplate::class, true) + ? $class + : null; } diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index abaa37e5a..69085e152 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -561,7 +561,7 @@ public static function formatRenderMethod(string $view): string protected function createTemplate(): ITemplate { - return $this->getTemplateFactory()->createTemplate($this); + return $this->getTemplateFactory()->createTemplate($this, self::formatTemplateClass()); } diff --git a/src/Bridges/ApplicationLatte/TemplateFactory.php b/src/Bridges/ApplicationLatte/TemplateFactory.php index 6b9a217cd..89edd9b4e 100644 --- a/src/Bridges/ApplicationLatte/TemplateFactory.php +++ b/src/Bridges/ApplicationLatte/TemplateFactory.php @@ -54,10 +54,15 @@ public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $ht } - public function createTemplate(UI\Control $control = null): UI\ITemplate + public function createTemplate(UI\Control $control = null, string $class = null): UI\ITemplate { $latte = $this->latteFactory->create(); - $template = new $this->templateClass($latte); + $class = $class ?: $this->templateClass; + $template = new $class($latte); + if (!$template instanceof LatteTemplate) { + throw new Nette\InvalidArgumentException("Class $class does not extend " . LatteTemplate::class . '.'); + } + $presenter = $control ? $control->getPresenterIfExists() : null; if ($latte->onCompile instanceof \Traversable) {