Skip to content

Instantly share code, notes, and snippets.

@VottusCode
Created December 2, 2022 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save VottusCode/ddacea244ca6b50feece94181c6ca4e2 to your computer and use it in GitHub Desktop.
Save VottusCode/ddacea244ca6b50feece94181c6ca4e2 to your computer and use it in GitHub Desktop.
Extended template resolution with themes
<?php declare(strict_types=1);
namespace App\Templating;
use Nette\Application\UI\Presenter;
use Nette\Utils\FileSystem;
class TemplateHelper
{
public function __construct(
public string $theme = "default",
public string $baseDir = "/"
)
{
}
public function getThemeRoot(): string
{
return FileSystem::normalizePath(sprintf("%s/%s", $this->baseDir, $this->theme));
}
public function getRelativePresenterRoot(Presenter $presenter): string
{
// Replaces module:presenter separator with a path separator.
// This way non-moduled presenters can be used as well.
$presenterName = str_replace(":", "/", $presenter->getName() ?? '');
return FileSystem::normalizePath($presenterName);
}
public function getRelativeTemplate(Presenter $presenter): string
{
return FileSystem::normalizePath(sprintf("%s/%s", $this->getRelativePresenterRoot($presenter), $presenter->getView()));
}
}
<?php declare(strict_types=1);
namespace App\Presenters\Traits;
use App\Templating\TemplateHelper;
use Nette\Application\Helpers;
use Nette\Utils\FileSystem;
trait UsesTemplating
{
private TemplateHelper $templateHelper;
public function injectTemplateHelper(TemplateHelper $helper): void
{
$this->templateHelper = $helper;
}
/**
* Formats layout template file names.
*/
public function formatLayoutTemplateFiles(): array
{
if (preg_match('#/|\\\\#', (string) $this->layout)) {
return [$this->layout];
}
$themeRoot = $this->templateHelper->getThemeRoot();
$presenterRoot = $this->templateHelper->getRelativePresenterRoot($this);
$layout = $this->layout ?: 'layout';
[$module, $presenter] = Helpers::splitName($this->getName() ?? '');
$list = array_map(fn($path) => FileSystem::normalizePath($path), [
"$themeRoot/$presenterRoot/@$layout.latte",
"$themeRoot/$presenterRoot/$presenter.@$layout.latte",
"$themeRoot/$module/$presenter.@$layout.latte",
"$themeRoot/$module/@$layout.latte",
"$themeRoot/@$layout.latte"
]);
return $list;
}
/**
* Formats view template file names.
*/
public function formatTemplateFiles(): array
{
$themeRoot = $this->templateHelper->getThemeRoot();
$presenterRoot = $this->templateHelper->getRelativePresenterRoot($this);
$template = $this->templateHelper->getRelativeTemplate($this);
$view = $this->getView();
[$module, $presenter] = Helpers::splitName($this->getName());
return array_map(fn($path) => FileSystem::normalizePath($path), [
"$themeRoot/$template.latte",
"$themeRoot/$presenterRoot/$view.latte",
"$themeRoot/$module/$presenter.$view.latte"
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment