Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  
  26  namespace core\output;
  27  
  28  use Mustache_LambdaHelper;
  29  use 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   */
  38  class 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          $text = strtok("");
  72          // Allow mustache tags in the last argument.
  73          $text = trim($helper->render($text));
  74          // The $text has come from a template, so HTML special
  75          // chars have been escaped. However, render_pix_icon
  76          // assumes the alt arrives with no escaping. So we need
  77          // ot un-escape here.
  78          $text = htmlspecialchars_decode($text);
  79  
  80          return trim($this->renderer->pix_icon($key, $text, $component));
  81      }
  82  }
  83