diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-13 13:44:21 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-08-13 13:44:21 +0400 |
commit | 42fb686b6708f3e5ce3a1364681c9d182d771ca3 (patch) | |
tree | e15d50cddf7d635898a09fe398ed78af6a168024 /core | |
parent | 1d2524063fd84ca90d9dbbf09962e9e780c4dc0d (diff) |
added method again with a better - and more unique - name
Diffstat (limited to 'core')
-rw-r--r-- | core/Plugin/Controller.php | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/core/Plugin/Controller.php b/core/Plugin/Controller.php index 5521eaf0c2..7b6298dfe7 100644 --- a/core/Plugin/Controller.php +++ b/core/Plugin/Controller.php @@ -249,6 +249,45 @@ abstract class Controller } /** + * Assigns the given variables to the template and renders it. + * + * Example: + * ``` + public function myControllerAction () { + return $this->renderTemplate('index', array( + 'answerToLife' => '42' + )); + } + ``` + * This will render the 'index.twig' file within the plugin templates folder and assign the view variable + * `answerToLife` to `42`. + * + * @param string $template The name of the template file. If only a name is given it will automatically use + * the template within the plugin folder. For instance 'myTemplate' will result in + * '@$pluginName/myTemplate.twig'. Alternatively you can include the full path: + * '@anyOtherFolder/otherTemplate'. The trailing '.twig' is not needed. + * @param array $variables For instance array('myViewVar' => 'myValue'). In template you can use {{ myViewVar }} + * @return string + * @since 2.5.0 + * @api + */ + protected function renderTemplate($template, array $variables = array()) + { + if (false === strpos($template, '@') || false === strpos($template, '/')) { + $template = '@' . $this->pluginName . '/' . $template; + } + + $view = new View($template); + $this->setBasicVariablesView($view); + + foreach ($variables as $key => $value) { + $view->$key = $value; + } + + return $view->render(); + } + + /** * Convenience method that creates and renders a ViewDataTable for a API method. * * @param string|\Piwik\Plugin\Report $apiAction The name of the API action (eg, `'getResolution'`) or |