Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@googlemail.com>2014-08-13 12:26:50 +0400
committerThomas Steur <thomas.steur@googlemail.com>2014-08-13 12:26:50 +0400
commit25d388ca505764de547205aa1b0db0a65f056798 (patch)
tree88769e6fc96287ed52dc37fc2a62540cd68e98e9 /core/Plugin
parent84e39a48f983bf7d7b209fba7a4a09c4da9e517d (diff)
adding a convenient method to render a template and to assign variables. We are always doing the same in the controllers there and this can be simplified this way. So developers do no longer need to know about setBasicVariables(), about the View object etc. Easy! We need to document how developers can extend an admin template (extend admin.twig + block content) and how to extend a regular dashboard template
Diffstat (limited to 'core/Plugin')
-rw-r--r--core/Plugin/Controller.php39
1 files changed, 39 insertions, 0 deletions
diff --git a/core/Plugin/Controller.php b/core/Plugin/Controller.php
index 5521eaf0c2..bd3ef0d3bf 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->render('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 render($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