Skip to content

Commit

Permalink
Presenter: try to find Template file for each action (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
lulco authored and dg committed Feb 5, 2021
1 parent ac400c4 commit b4ba952
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,16 @@ protected function createTemplate(/*string $class = null*/): Template
}


public function formatTemplateClass(): ?string
{
$class = preg_replace('#Presenter$#', ucfirst((string) $this->action) . 'Template', static::class);
if (class_exists($class) && is_a($class, Template::class, true)) {
return $class;
}
return parent::formatTemplateClass();
}


/********************* partial AJAX rendering ****************d*g**/


Expand Down
13 changes: 13 additions & 0 deletions tests/UI/Presenter.formatTemplateClass.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class CTemplate implements Nette\Application\UI\Template
}
}

class CBarTemplate extends CTemplate
{
}

test('without template', function () {
$presenter = new APresenter;
Expand All @@ -64,3 +67,13 @@ test('with template', function () {
$presenter = new CPresenter;
Assert::same(CTemplate::class, $presenter->formatTemplateClass());
});


test('with action template', function () {
$presenter = new CPresenter;
$presenter->changeAction('Foo');
Assert::same(CTemplate::class, $presenter->formatTemplateClass());

$presenter->changeAction('Bar');
Assert::same(CBarTemplate::class, $presenter->formatTemplateClass());
});

0 comments on commit b4ba952

Please sign in to comment.