MDL-49152 output: Templates for renderers (JS and PHP)

Mustache support for rendering templates from either php or js.
This commit is contained in:
Damyon Wiese
2015-03-14 22:48:40 +08:00
parent 72f8324ec5
commit 9bdcf57951
66 changed files with 6532 additions and 33 deletions
+133
View File
@@ -0,0 +1,133 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mustache helper to load strings from string_manager.
*
* @package core
* @category output
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
use external_api;
use external_function_parameters;
use external_value;
use core_component;
use moodle_exception;
use context_system;
use theme_config;
/**
* This class contains a list of webservice functions related to output.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
class external extends external_api {
/**
* Returns description of load_template() parameters.
*
* @return external_function_parameters
*/
public static function load_template_parameters() {
return new external_function_parameters(
array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
'template' => new external_value(PARAM_ALPHANUMEXT, 'name of the template'),
'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
)
);
}
/**
* Can this function be called directly from ajax?
*
* @return boolean
* @since Moodle 2.9
*/
public static function load_template_is_allowed_from_ajax() {
return true;
}
/**
* Return a mustache template, and all the strings it requires.
*
* @param string $component The component that holds the template.
* @param string $templatename The name of the template.
* @param string $themename The name of the current theme.
* @return string the template
*/
public static function load_template($component, $template, $themename) {
global $DB, $CFG, $PAGE;
$params = self::validate_parameters(self::load_template_parameters(),
array('component' => $component,
'template' => $template,
'themename' => $themename));
$component = $params['component'];
$template = $params['template'];
$themename = $params['themename'];
// Check if this is a valid component.
$componentdir = core_component::get_component_directory($component);
if (empty($componentdir)) {
throw new moodle_exception('filenotfound', 'error');
}
// Places to look.
$candidates = array();
// Theme dir.
$root = $CFG->dirroot;
$themeconfig = theme_config::load($themename);
$candidate = "${root}/theme/${themename}/templates/${component}/${template}.mustache";
$candidates[] = $candidate;
// Theme parents dir.
foreach ($themeconfig->parents as $theme) {
$candidate = "${root}/theme/${theme}/templates/${component}/${template}.mustache";
$candidates[] = $candidate;
}
// Component dir.
$candidate = "${componentdir}/templates/${template}.mustache";
$candidates[] = $candidate;
$templatestr = false;
foreach ($candidates as $candidate) {
if (file_exists($candidate)) {
$templatestr = file_get_contents($candidate);
break;
}
}
if ($templatestr === false) {
throw new moodle_exception('filenotfound', 'error');
}
return $templatestr;
}
/**
* Returns description of load_template() result value.
*
* @return external_description
*/
public static function load_template_returns() {
return new external_value(PARAM_RAW, 'template');
}
}
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Perform some custom name mapping for template file names (strip leading component/).
*
* @package core
* @category output
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
use coding_exception;
/**
* Perform some custom name mapping for template file names (strip leading component/).
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
class mustache_filesystem_loader extends \Mustache_Loader_FilesystemLoader {
/**
* Helper function for getting a Mustache template file name.
* Strips the leading component as we are already limited to the correct directories.
*
* @param string $name
*
* @return string Template file name
*/
protected function getFileName($name) {
if (strpos($name, '/') === false) {
throw new coding_exception('Templates names must be specified as "componentname/templatename" (' . $name . ' requested) ');
}
list($component, $templatename) = explode('/', $name, 2);
return parent::getFileName($templatename);
}
}
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mustache helper that will add JS to the end of the page.
*
* @package core
* @category output
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
/**
* Store a list of JS calls to insert at the end of the page.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
class mustache_javascript_helper {
/** @var page_requirements_manager $requires - Page requirements manager for collecting JS calls. */
private $requires = null;
/**
* Create new instance of mustache javascript helper.
*
* @param page_requirements_manager $requires Page requirements manager.
*/
public function __construct($requires) {
$this->requires = $requires;
}
/**
* Add the block of text to the page requires so it is appended in the footer. The
* content of the block can contain further mustache tags which will be resolved.
*
* @param string $text The script content of the section.
* @param \Mustache_LambdaHelper $helper Used to render the content of this block.
*/
public function help($text, \Mustache_LambdaHelper $helper) {
$this->requires->js_amd_inline($helper->render($text));
}
}
@@ -0,0 +1,78 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mustache helper render pix icons.
*
* @package core
* @category output
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
use Mustache_LambdaHelper;
use renderer_base;
/**
* This class will call pix_icon with the section content.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
class mustache_pix_helper {
/** @var renderer_base $renderer A reference to the renderer in use */
private $renderer;
/**
* Save a reference to the renderer.
* @param renderer_base $renderer
*/
public function __construct(renderer_base $renderer) {
$this->renderer = $renderer;
}
/**
* Read a pix icon name from a template and get it from pix_icon.
*
* {{#pix}}t/edit,component,Anything else is alt text{{/pix}}
*
* The args are comma separated and only the first is required.
*
* @param string $text The text to parse for arguments.
* @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
* @return string
*/
public function pix($text, Mustache_LambdaHelper $helper) {
// Split the text into an array of variables.
$key = strtok($text, ",");
$key = trim($key);
$component = strtok(",");
$component = trim($component);
if (!$component) {
$component = '';
}
$text = strtok("");
// Allow mustache tags in the last argument.
$text = $helper->render($text);
return trim($this->renderer->pix_icon($key, $text, $component));
}
}
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mustache helper to load strings from string_manager.
*
* @package core
* @category output
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
use Mustache_LambdaHelper;
use stdClass;
/**
* This class will load language strings in a template.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
class mustache_string_helper {
/**
* Read a lang string from a template and get it from get_string.
*
* Some examples for calling this from a template are:
*
* {{#str}}activity{{/str}}
* {{#str}}actionchoice, core, {{#str}}delete{{/str}}{{/str}} (Nested)
* {{#str}}addinganewto, core, {"what":"This", "to":"That"}{{/str}} (Complex $a)
*
* The args are comma separated and only the first is required.
* The last is a $a argument for get string. For complex data here, use JSON.
*
* @param string $text The text to parse for arguments.
* @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
* @return string
*/
public function str($text, Mustache_LambdaHelper $helper) {
// Split the text into an array of variables.
$key = strtok($text, ",");
$key = trim($key);
$component = strtok(",");
$component = trim($component);
if (!$component) {
$component = '';
}
$a = new stdClass();
$next = strtok('');
$next = trim($next);
if ((strpos($next, '{') === 0) && (strpos($next, '{{') !== 0)) {
$rawjson = $helper->render($next);
$a = json_decode($rawjson);
} else {
$a = $helper->render($next);
}
return get_string($key, $component, $a);
}
}
@@ -0,0 +1,52 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mustache helper that will add JS to the end of the page.
*
* @package core
* @category output
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
/**
* Lazy create a uniqid per instance of the class. The id is only generated
* when this class it converted to a string.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.9
*/
class mustache_uniqid_helper {
/** @var string $uniqid The unique id */
private $uniqid = null;
/**
* Init the random variable and return it as a string.
*
* @return string random id.
*/
public function __toString() {
if ($this->uniqid === null) {
$this->uniqid = \html_writer::random_id(uniqid());
}
return $this->uniqid;
}
}