Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core\output;
18
 
19
use Mustache_LambdaHelper;
20
use stdClass;
21
 
22
/**
23
 * This class will load language strings in a template.
24
 *
25
 * @copyright  2015 Damyon Wiese
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @since      2.9
1441 ariadna 28
 * @package core
1 efrain 29
 */
30
class mustache_string_helper {
31
    /**
32
     * Read a lang string from a template and get it from get_string.
33
     *
34
     * Some examples for calling this from a template are:
35
     *
36
     * {{#str}}activity{{/str}}
37
     * {{#str}}actionchoice, core, {{#str}}delete{{/str}}{{/str}} (Nested)
38
     * {{#str}}addinganewto, core, {"what":"This", "to":"That"}{{/str}} (Complex $a)
39
     *
40
     * The args are comma separated and only the first is required.
41
     * The last is a $a argument for get string. For complex data here, use JSON.
42
     *
43
     * @param string $text The text to parse for arguments.
44
     * @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
45
     * @return string
46
     */
47
    public function str($text, Mustache_LambdaHelper $helper) {
48
        // Split the text into an array of variables.
49
        $key = strtok($text, ",");
50
        $key = trim($key);
51
        $component = strtok(",");
52
        $component = trim($component);
53
        if (!$component) {
54
            $component = '';
55
        }
56
 
57
        $a = new stdClass();
58
 
59
        $next = strtok('');
60
        $next = trim($next);
61
        if ((strpos($next, '{') === 0) && (strpos($next, '{{') !== 0)) {
62
            $rawjson = $helper->render($next);
63
            $a = json_decode($rawjson);
64
        } else {
65
            $a = $helper->render($next);
66
        }
67
        return get_string($key, $component, $a);
68
    }
69
}