Skip to content

Commit

Permalink
Control, Presenter::createTemplate() tries to create custom Template …
Browse files Browse the repository at this point in the history
…according to naming convention
  • Loading branch information
dg committed Jan 5, 2020
1 parent 961f708 commit ba8a32e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/Application/UI/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}


Expand Down
9 changes: 7 additions & 2 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ba8a32e

Please sign in to comment.