Piwik\Plugin\Controller::renderTemplate PHP Method

renderTemplate() protected method

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.
Since: 2.5.0
protected renderTemplate ( string $template, array $variables = [] ) : string
$template string 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.
$variables array For instance array('myViewVar' => 'myValue'). In template you can use {{ myViewVar }}
return string
    protected function renderTemplate($template, array $variables = array())
    {
        if (false === strpos($template, '@') || false === strpos($template, '/')) {
            $template = '@' . $this->pluginName . '/' . $template;
        }
        $view = new View($template);
        // alternatively we could check whether the templates extends either admin.twig or dashboard.twig and based on
        // that call the correct method. This will be needed once we unify Controller and ControllerAdmin see
        // https://github.com/piwik/piwik/issues/6151
        if ($this instanceof ControllerAdmin) {
            $this->setBasicVariablesView($view);
        } elseif (empty($this->site) || empty($this->idSite)) {
            $this->setBasicVariablesView($view);
        } else {
            $this->setGeneralVariablesView($view);
        }
        foreach ($variables as $key => $value) {
            $view->{$key} = $value;
        }
        return $view->render();
    }