Skip to content

Instantly share code, notes, and snippets.

@VaclavSir
Created January 22, 2014 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VaclavSir/8559652 to your computer and use it in GitHub Desktop.
Save VaclavSir/8559652 to your computer and use it in GitHub Desktop.
<?php
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
/**
* Path to theme templates.
*
* @var string
*/
private $themePath;
/**
* Set a theme.
* @param string $themePath
*/
public function setThemePath($themePath)
{
$this->themePath = $themePath;
}
protected function createTemplate($class = NULL)
{
$template = parent::createTemplate($class);
foreach ($this->formatDefaultViewTemplateFileNames() as $defaultViewFileName) {
if (is_file($defaultViewFileName)) {
$template->baseViewTemplate = $defaultViewFileName;
}
}
foreach ($this->formatDefaultLayoutTemplateFileNames() as $defaultLayoutFileName) {
if (is_file($defaultLayoutFileName)) {
$template->baseLayoutTemplate = $defaultLayoutFileName;
}
}
return $template;
}
/**
* Get the list of possible default view template files.
* @return array
*/
public function formatDefaultViewTemplateFileNames()
{
return parent::formatTemplateFiles();
}
/**
* Get the list of default layout templates.
* @return array
*/
public function formatDefaultLayoutTemplateFileNames()
{
return parent::formatLayoutTemplateFiles();
}
/**
* Get the list of possible view templates from the current theme.
* @return array
*/
public function formatThemeViewTemplateFileNames()
{
if (empty($this->themePath)) {
return array();
}
$defaultPathLength = strlen(dirname(dirname($this->getReflection()->getFileName())) . '/templates');
$themePath = $this->themePath;
return array_map(function ($filename) use ($defaultPathLength, $themePath) {
return $themePath . substr($filename, $defaultPathLength);
}, $this->formatDefaultViewTemplateFileNames());
}
/**
* Get the list of possible theme layout template files.
* @return array
*/
public function formatThemeLayoutTemplateFileNames()
{
if (empty($this->themePath)) {
return array();
}
$defaultPathLength = strlen(dirname(dirname($this->getReflection()->getFileName())) . '/templates');
$themePath = $this->themePath;
return array_map(function ($filename) use ($defaultPathLength, $themePath) {
return $themePath . substr($filename, $defaultPathLength);
}, $this->formatDefaultLayoutTemplateFileNames());
}
/**
* @return array
*/
public function formatTemplateFiles()
{
return array_merge(
$this->formatThemeViewTemplateFileNames(),
$this->formatDefaultViewTemplateFileNames()
);
}
/**
* @return array
*/
public function formatLayoutTemplateFiles()
{
return array_merge(
$this->formatThemeLayoutTemplateFileNames(),
$this->formatDefaultLayoutTemplateFileNames()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment