1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Mustache helper render pix icons.
19 *
20 * @package    core
21 * @category   output
22 * @copyright  2015 Damyon Wiese
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core\output;
27
28use Mustache_LambdaHelper;
29use renderer_base;
30
31/**
32 * This class will call pix_icon with the section content.
33 *
34 * @copyright  2015 Damyon Wiese
35 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @since      2.9
37 */
38class mustache_pix_helper {
39
40    /** @var renderer_base $renderer A reference to the renderer in use */
41    private $renderer;
42
43    /**
44     * Save a reference to the renderer.
45     * @param renderer_base $renderer
46     */
47    public function __construct(renderer_base $renderer) {
48        $this->renderer = $renderer;
49    }
50
51    /**
52     * Read a pix icon name from a template and get it from pix_icon.
53     *
54     * {{#pix}}t/edit,component,Anything else is alt text{{/pix}}
55     *
56     * The args are comma separated and only the first is required.
57     *
58     * @param string $text The text to parse for arguments.
59     * @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
60     * @return string
61     */
62    public function pix($text, Mustache_LambdaHelper $helper) {
63        // Split the text into an array of variables.
64        $key = strtok($text, ",");
65        $key = trim($helper->render($key));
66        $component = strtok(",");
67        $component = trim($helper->render($component));
68        if (!$component) {
69            $component = '';
70        }
71        $component = $helper->render($component);
72        $text = strtok("");
73        // Allow mustache tags in the last argument.
74        $text = trim($helper->render($text));
75        // The $text has come from a template, so HTML special
76        // chars have been escaped. However, render_pix_icon
77        // assumes the alt arrives with no escaping. So we need
78        // ot un-escape here.
79        $text = htmlspecialchars_decode($text);
80
81        return trim($this->renderer->pix_icon($key, $text, $component));
82    }
83}
84
85