Allow to use the original template in overridden templates (PR #1941)

This commit is contained in:
Frederic Guillot
2016-03-25 18:19:31 -04:00
parent 354e37971d
commit 407a51e6c4
4 changed files with 38 additions and 24 deletions

View File

@@ -9,6 +9,7 @@ New features:
Improvements: Improvements:
* Allow to use the original template in overridden templates
* Unification of the project header * Unification of the project header
* Refactoring of Javascript code * Refactoring of Javascript code
* Improve comments design * Improve comments design

View File

@@ -84,25 +84,26 @@ class Template
/** /**
* Find template filename * Find template filename
* *
* Core template name: 'task/show' * Core template: 'task/show' or 'kanboard:task/show'
* Plugin template name: 'myplugin:task/show' * Plugin template: 'myplugin:task/show'
* *
* @access public * @access public
* @param string $template_name * @param string $template
* @return string * @return string
*/ */
public function getTemplateFile($template_name) public function getTemplateFile($template)
{ {
$template_name = isset($this->overrides[$template_name]) ? $this->overrides[$template_name] : $template_name; $plugin = '';
$template = isset($this->overrides[$template]) ? $this->overrides[$template] : $template;
if (strpos($template_name, ':') !== false) { if (strpos($template, ':') !== false) {
list($plugin, $template) = explode(':', $template_name); list($plugin, $template) = explode(':', $template);
$path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'plugins';
$path .= DIRECTORY_SEPARATOR.ucfirst($plugin).DIRECTORY_SEPARATOR.'Template'.DIRECTORY_SEPARATOR.$template.'.php';
} else {
$path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Template'.DIRECTORY_SEPARATOR.$template_name.'.php';
} }
return $path; if ($plugin !== 'kanboard' && $plugin !== '') {
return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', '..', 'plugins', ucfirst($plugin), 'Template', $template.'.php'));
}
return implode(DIRECTORY_SEPARATOR, array(__DIR__, '..', 'Template', $template.'.php'));
} }
} }

View File

@@ -34,3 +34,9 @@ $this->template->setTemplateOverride('header', 'theme:layout/header');
``` ```
The first argument is the original template name and the second argument the template to use as replacement. The first argument is the original template name and the second argument the template to use as replacement.
You can still use the original template using the "kanboard:" prefix:
```php
<?= $this->render('kanboard:header') ?>
```

View File

@@ -8,35 +8,41 @@ class TemplateTest extends Base
{ {
public function testGetTemplateFile() public function testGetTemplateFile()
{ {
$t = new Template($this->container['helper']); $template = new Template($this->container['helper']);
$this->assertStringEndsWith( $this->assertStringEndsWith(
'app'.DIRECTORY_SEPARATOR.'Core'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Template'.DIRECTORY_SEPARATOR.'a'.DIRECTORY_SEPARATOR.'b.php', implode(DIRECTORY_SEPARATOR, array('app', 'Core', '..', 'Template', 'a', 'b.php')),
$t->getTemplateFile('a'.DIRECTORY_SEPARATOR.'b') $template->getTemplateFile('a'.DIRECTORY_SEPARATOR.'b')
);
$this->assertStringEndsWith(
implode(DIRECTORY_SEPARATOR, array('app', 'Core', '..', 'Template', 'a', 'b.php')),
$template->getTemplateFile('kanboard:a'.DIRECTORY_SEPARATOR.'b')
); );
} }
public function testGetPluginTemplateFile() public function testGetPluginTemplateFile()
{ {
$t = new Template($this->container['helper']); $template = new Template($this->container['helper']);
$this->assertStringEndsWith( $this->assertStringEndsWith(
'app'.DIRECTORY_SEPARATOR.'Core'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'Myplugin'.DIRECTORY_SEPARATOR.'Template'.DIRECTORY_SEPARATOR.'a'.DIRECTORY_SEPARATOR.'b.php', implode(DIRECTORY_SEPARATOR, array('app', 'Core', '..', '..', 'plugins', 'Myplugin', 'Template', 'a', 'b.php')),
$t->getTemplateFile('myplugin:a'.DIRECTORY_SEPARATOR.'b') $template->getTemplateFile('myplugin:a'.DIRECTORY_SEPARATOR.'b')
); );
} }
public function testGetOverridedTemplateFile() public function testGetOverridedTemplateFile()
{ {
$t = new Template($this->container['helper']); $template = new Template($this->container['helper']);
$t->setTemplateOverride('a'.DIRECTORY_SEPARATOR.'b', 'myplugin:c'); $template->setTemplateOverride('a'.DIRECTORY_SEPARATOR.'b', 'myplugin:c');
$this->assertStringEndsWith( $this->assertStringEndsWith(
'app'.DIRECTORY_SEPARATOR.'Core'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'Myplugin'.DIRECTORY_SEPARATOR.'Template'.DIRECTORY_SEPARATOR.'c.php', implode(DIRECTORY_SEPARATOR, array('app', 'Core', '..', '..', 'plugins', 'Myplugin', 'Template', 'c.php')),
$t->getTemplateFile('a'.DIRECTORY_SEPARATOR.'b') $template->getTemplateFile('a'.DIRECTORY_SEPARATOR.'b')
); );
$this->assertStringEndsWith( $this->assertStringEndsWith(
'app'.DIRECTORY_SEPARATOR.'Core'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Template'.DIRECTORY_SEPARATOR.'d.php', implode(DIRECTORY_SEPARATOR, array('app', 'Core', '..', 'Template', 'd.php')),
$t->getTemplateFile('d') $template->getTemplateFile('d')
); );
} }
} }