1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * This is called from to render mustache templates
19 * @module tool_componentlibrary/mustache
20 * @package tool_componentlibrary
21 * @copyright 2021 Bas Brands <bas@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 import selectors from 'tool_componentlibrary/selectors';
26 import Ajax from 'core/ajax';
27 import Config from 'core/config';
28 import Templates from 'core/templates';
29 import Log from 'core/log';
30 import Notification from 'core/notification';
33 * Handle a template loaded response.
35 * @param {String} container The template container
36 * @param {String} templateName The template name
37 * @param {String} context Data for the template.
39 const renderTemplate = async(container, templateName, context) => {
41 context = JSON.parse(context);
43 Log.debug('Could not parse json example context for template.');
47 const {html, js} = await Templates.renderForPromise(templateName, context);
49 const rendercontainer = container.querySelector(selectors.mustacherendered);
51 // Load the rendered content in the renderer tab.
52 await Templates.replaceNodeContents(rendercontainer, html, js);
56 * Load the a template source from Moodle.
58 * @param {String} container The template container
60 const loadTemplate = container => {
61 const sourcecontainer = container.querySelector(selectors.mustachesource);
62 const contextcontainer = container.querySelector(selectors.mustachecontext);
63 const templateName = container.dataset.template;
64 let context = container.querySelector(selectors.mustacherawcontext).textContent;
66 const parts = templateName.split('/');
67 const component = parts.shift();
68 const name = parts.join('/');
71 methodname: 'core_output_load_template',
75 themename: Config.theme,
80 Ajax.call([request])[0]
82 // Load the source template in Template tab.
83 sourcecontainer.textContent = source;
85 const example = source.match(/Example context \(json\):([\s\S]+?)(}})/);
87 // Load the variables in the Variables tab.
88 const precontainer = document.createElement("pre");
89 precontainer.innerHTML = JSON.stringify(JSON.parse(context), null, 4);
90 contextcontainer.parentNode.appendChild(precontainer);
91 contextcontainer.classList.add('d-none');
93 renderTemplate(container, templateName, context);
95 .fail(Notification.exception);
102 export const mustache = () => {
103 document.querySelectorAll(selectors.mustachecode).forEach((container) => {
104 loadTemplate(container);