aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/TemplateWidget.php
blob: 391d5afa00fe9ddc846bfb9b12898f4ba856723a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
 * This widget type consists of a template and associated variables and will
 * render the template content as the widget content.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 4.0
 */
class TemplateWidget extends SidebarWidget
{
    protected $template;

    /**
     * Constructor of the widget.
     *
     * @param String         $title     Title of the widget
     * @param Flexi_Template $template  Template for the widget
     * @param array          $variables Associated variables for the template
     */
    public function __construct($title, Flexi_Template $template, array $variables = [])
    {
        parent::__construct();

        $this->title    = $title;
        $this->template = $template;
        $this->template->set_attributes($variables);
    }

    /**
     * Renders the template and widget.
     *
     * @param array $variables Additional variables for rendering
     * @return string containing the rendered widget as html
     */
    public function render($variables = [])
    {
        $this->template->set_attributes($variables);
        $layout = $GLOBALS['template_factory']->open($this->layout);
        $layout->layout_css_classes = $this->layout_css_classes;
        return $this->template->render(
            $this->template_variables,
            $layout
        );
    }
}