Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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 to load strings from string_manager.
  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 stdClass;
  30  
  31  /**
  32   * This class will load language strings in a template.
  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_string_helper {
  39  
  40      /**
  41       * Read a lang string from a template and get it from get_string.
  42       *
  43       * Some examples for calling this from a template are:
  44       *
  45       * {{#str}}activity{{/str}}
  46       * {{#str}}actionchoice, core, {{#str}}delete{{/str}}{{/str}} (Nested)
  47       * {{#str}}addinganewto, core, {"what":"This", "to":"That"}{{/str}} (Complex $a)
  48       *
  49       * The args are comma separated and only the first is required.
  50       * The last is a $a argument for get string. For complex data here, use JSON.
  51       *
  52       * @param string $text The text to parse for arguments.
  53       * @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
  54       * @return string
  55       */
  56      public function str($text, Mustache_LambdaHelper $helper) {
  57          // Split the text into an array of variables.
  58          $key = strtok($text, ",");
  59          $key = trim($key);
  60          $component = strtok(",");
  61          $component = trim($component);
  62          if (!$component) {
  63              $component = '';
  64          }
  65  
  66          $a = new stdClass();
  67  
  68          $next = strtok('');
  69          $next = trim($next);
  70          if ((strpos($next, '{') === 0) && (strpos($next, '{{') !== 0)) {
  71              $rawjson = $helper->render($next);
  72              $a = json_decode($rawjson);
  73          } else {
  74              $a = $helper->render($next);
  75          }
  76          return get_string($key, $component, $a);
  77      }
  78  }