diff options
author | root <root@test1.datamints.local> | 2014-04-24 18:18:53 +0400 |
---|---|---|
committer | root <root@test1.datamints.local> | 2014-04-24 18:18:53 +0400 |
commit | 6899ba9ed1b2cb78cb2ffc0578c6cee21d8fad4c (patch) | |
tree | c1765004e135429a82f563ae40a7e427aa035791 /core/Twig.php | |
parent | 135624da26803c8af43ba4299add7650554036ee (diff) |
Added possibility to modify twig templates in own theme.
Theme templates can be overridden by placing them in plugins/[theme]/templates.
Plugin-templates can be overridden by placing themb in plugins/[theme]/templates/plugin/[plugin]/
Diffstat (limited to 'core/Twig.php')
-rwxr-xr-x[-rw-r--r--] | core/Twig.php | 56 |
1 files changed, 51 insertions, 5 deletions
diff --git a/core/Twig.php b/core/Twig.php index 16348e524c..5a260ccbb0 100644..100755 --- a/core/Twig.php +++ b/core/Twig.php @@ -37,11 +37,25 @@ class Twig public function __construct() { $loader = $this->getDefaultThemeLoader(); - - $this->addPluginNamespaces($loader); - - // If theme != default we need to chain - $chainLoader = new Twig_Loader_Chain(array($loader)); + $this->addPluginNamespaces($loader); + + //get current theme + $manager = Plugin\Manager::getInstance(); + $theme = $manager->getThemeEnabled(); + $loaders = array(); + + //create loader for custom theme to overwrite twig templates + if($theme->getPluginName() != \Piwik\Plugin\Manager::DEFAULT_THEME){ + $customLoader = $this->getCustomThemeLoader($theme); + if($customLoader){ + //make it possible to overwrite plugin templates + $this->addCustomPluginNamespaces($customLoader,$theme->getPluginName()); + $loaders[] = $customLoader; + } + } + $loaders[] = $loader; + + $chainLoader = new Twig_Loader_Chain($loaders); // Create new Twig Environment and set cache dir $templatesCompiledPath = PIWIK_USER_PATH . '/tmp/templates_c'; @@ -164,6 +178,22 @@ class Twig return $themeLoader; } + /** + * create template loader for a custom theme + * @param \Piwik\Plugin $theme + * @return \Twig_Loader_Filesystem + */ + protected function getCustomThemeLoader(Plugin $theme){ + if(!file_exists(sprintf("%s/plugins/%s/templates/", PIWIK_INCLUDE_PATH, $theme->getPluginName()))){ + return false; + } + $themeLoader = new Twig_Loader_Filesystem(array( + sprintf("%s/plugins/%s/templates/", PIWIK_INCLUDE_PATH, $theme->getPluginName()) + )); + + return $themeLoader; + } + public function getTwigEnvironment() { return $this->twig; @@ -289,6 +319,22 @@ class Twig } } + /** + * + * Plugin-Templates can be overwritten by putting identically named templates in plugins/[theme]/templates/plugins/[plugin]/ + * + */ + private function addCustomPluginNamespaces(Twig_Loader_Filesystem $loader, $pluginName) + { + $plugins = \Piwik\Plugin\Manager::getInstance()->getAllPluginsNames(); + foreach ($plugins as $name) { + $path = sprintf("%s/plugins/%s/templates/plugins/%s/", PIWIK_INCLUDE_PATH, $pluginName, $name); + if (is_dir($path)) { + $loader->addPath(PIWIK_INCLUDE_PATH . '/plugins/' . $pluginName . '/templates/plugins/'. $name , $name); + } + } + } + /** * Prepend relative paths with absolute Piwik path * |