Skip to content

Commit

Permalink
Presenter: support for templates without /template folder
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 14, 2024
1 parent 4fea67f commit d6d72b7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 30 deletions.
30 changes: 24 additions & 6 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,28 @@ public function formatLayoutTemplateFiles(): array
return [$this->layout];
}
[$module, $presenter] = Helpers::splitName($this->getName());
$layout = $this->layout ?: 'layout';
$dir = dirname(static::getReflection()->getFileName());
$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
$levels = substr_count($this->getName(), ':');
if (!is_dir("$dir/templates")) {
$dir = dirname($origDir = $dir);
if (!is_dir("$dir/templates")) {
$list = ["$origDir/@$layout.latte"];
do {
$list[] = "$dir/@$layout.latte";
} while ($levels-- && ($dir = dirname($dir)));
return $list;
}
}

[, $presenter] = Helpers::splitName($this->getName());
$list = [
"$dir/templates/$presenter/@$layout.latte",
"$dir/templates/$presenter.@$layout.latte",
];
do {
$list[] = "$dir/templates/@$layout.latte";
$dir = dirname($dir);
} while ($dir && $module && ([$module] = Helpers::splitName($module)));
} while ($levels-- && ($dir = dirname($dir)));

return $list;
}
Expand All @@ -585,9 +595,17 @@ public function formatLayoutTemplateFiles(): array
*/
public function formatTemplateFiles(): array
{
[, $presenter] = Helpers::splitName($this->getName());
$dir = dirname(static::getReflection()->getFileName());
$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
if (!is_dir("$dir/templates")) {
$dir = dirname($origDir = $dir);
if (!is_dir("$dir/templates")) {
return [
"$origDir/$this->view.latte",
];
}
}

[, $presenter] = Helpers::splitName($this->getName());
return [
"$dir/templates/$presenter/$this->view.latte",
"$dir/templates/$presenter.$this->view.latte",
Expand Down
46 changes: 36 additions & 10 deletions tests/UI/Presenter.formatLayoutTemplateFiles.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/one/APresenter.php';
require __DIR__ . '/two/BPresenter.php';
require __DIR__ . '/one/sub/BPresenter.php';
require __DIR__ . '/two/CPresenter.php';


test('with subdir templates', function () {
Expand All @@ -27,14 +28,26 @@ test('with subdir templates', function () {
});


test('without subdir templates', function () {
test('with parent-dir templates', function () {
$presenter = new BPresenter;
$presenter->setParent(null, 'One');
$presenter->setLayout('my');

Assert::same([
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/One/@my.latte',
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/[email protected]',
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/@my.latte',
], $presenter->formatLayoutTemplateFiles());
});


test('without subdir templates', function () {
$presenter = new CPresenter;
$presenter->setParent(null, 'Two');

Assert::same([
__DIR__ . '/templates/Two/@layout.latte',
__DIR__ . '/templates/[email protected]',
__DIR__ . '/templates/@layout.latte',
__DIR__ . DIRECTORY_SEPARATOR . 'two/@layout.latte',
__DIR__ . '/@layout.latte',
], $presenter->formatLayoutTemplateFiles());
});

Expand All @@ -53,16 +66,29 @@ test('with module & subdir templates', function () {
});


test('with module & without subdir templates', function () {
test('with module & parent-dir templates', function () {
$presenter = new BPresenter;
$presenter->setParent(null, 'Module:SubModule:Two');
$presenter->setParent(null, 'Module:SubModule:One');

Assert::same([
__DIR__ . '/templates/Two/@layout.latte',
__DIR__ . '/templates/[email protected]',
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/One/@layout.latte',
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/[email protected]',
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/@layout.latte',
__DIR__ . '/templates/@layout.latte',
dirname(__DIR__) . '/templates/@layout.latte',
dirname(__DIR__, 2) . '/templates/@layout.latte',
], $presenter->formatLayoutTemplateFiles());
});


test('with module & without subdir templates', function () {
$presenter = new CPresenter;
$presenter->setParent(null, 'Module:SubModule:Two');

Assert::same([
__DIR__ . DIRECTORY_SEPARATOR . 'two/@layout.latte',
__DIR__ . '/@layout.latte',
dirname(__DIR__) . '/@layout.latte',
dirname(__DIR__, 2) . '/@layout.latte',
], $presenter->formatLayoutTemplateFiles());
});

Expand Down
37 changes: 30 additions & 7 deletions tests/UI/Presenter.formatTemplateFiles.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/one/APresenter.php';
require __DIR__ . '/two/BPresenter.php';
require __DIR__ . '/one/sub/BPresenter.php';
require __DIR__ . '/two/CPresenter.php';


test('with subdir templates', function () {
Expand All @@ -26,14 +27,25 @@ test('with subdir templates', function () {
});


test('without subdir templates', function () {
test('with parent-dir templates', function () {
$presenter = new BPresenter;
$presenter->setParent(null, 'One');
$presenter->setView('view');

Assert::same([
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/One/view.latte',
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/One.view.latte',
], $presenter->formatTemplateFiles());
});


test('without subdir templates', function () {
$presenter = new CPresenter;
$presenter->setParent(null, 'Two');
$presenter->setView('view');

Assert::same([
__DIR__ . '/templates/Two/view.latte',
__DIR__ . '/templates/Two.view.latte',
__DIR__ . DIRECTORY_SEPARATOR . 'two/view.latte',
], $presenter->formatTemplateFiles());
});

Expand All @@ -50,13 +62,24 @@ test('with module & subdir templates', function () {
});


test('with module & without subdir templates', function () {
test('with module & parent-dir templates', function () {
$presenter = new BPresenter;
$presenter->setParent(null, 'Module:One');
$presenter->setView('view');

Assert::same([
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/One/view.latte',
__DIR__ . DIRECTORY_SEPARATOR . 'one/templates/One.view.latte',
], $presenter->formatTemplateFiles());
});


test('with module & without subdir templates', function () {
$presenter = new CPresenter;
$presenter->setParent(null, 'Module:Two');
$presenter->setView('view');

Assert::same([
__DIR__ . '/templates/Two/view.latte',
__DIR__ . '/templates/Two.view.latte',
__DIR__ . DIRECTORY_SEPARATOR . 'two/view.latte',
], $presenter->formatTemplateFiles());
});
7 changes: 7 additions & 0 deletions tests/UI/one/sub/BPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

class BPresenter extends Nette\Application\UI\Presenter
{
}
7 changes: 0 additions & 7 deletions tests/UI/two/BPresenter.php

This file was deleted.

7 changes: 7 additions & 0 deletions tests/UI/two/CPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

class CPresenter extends Nette\Application\UI\Presenter
{
}

0 comments on commit d6d72b7

Please sign in to comment.