Merge branch 'MDL-57742_master' of git://github.com/markn86/moodle
[moodle.git] / lib / classes / output / mustache_pix_helper.php
bloba6a330c9ba54074b768c5e344f394ae319f9d7bf
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.
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/>.
17 /**
18 * Mustache helper render pix icons.
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
26 namespace core\output;
28 use Mustache_LambdaHelper;
29 use renderer_base;
31 /**
32 * This class will call pix_icon with the section content.
34 * @copyright 2015 Damyon Wiese
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @since 2.9
38 class mustache_pix_helper {
40 /** @var renderer_base $renderer A reference to the renderer in use */
41 private $renderer;
43 /**
44 * Save a reference to the renderer.
45 * @param renderer_base $renderer
47 public function __construct(renderer_base $renderer) {
48 $this->renderer = $renderer;
51 /**
52 * Read a pix icon name from a template and get it from pix_icon.
54 * {{#pix}}t/edit,component,Anything else is alt text{{/pix}}
56 * The args are comma separated and only the first is required.
58 * @param string $text The text to parse for arguments.
59 * @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
60 * @return string
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 = '';
71 $component = $helper->render($component);
72 $text = strtok("");
73 // Allow mustache tags in the last argument.
74 $text = trim($helper->render($text));
76 return trim($this->renderer->pix_icon($key, $text, $component));