MDL-49650 tool_templatelibrary: New tool for browsing mustache templates

This commit is contained in:
Damyon Wiese 2015-03-25 00:05:06 +08:00
parent bed022decb
commit 274d79c9d5
20 changed files with 935 additions and 1 deletions

View File

@ -0,0 +1 @@
define(["jquery","core/ajax","core/log","core/notification","core/templates","core/config","core/str"],function(a,b,c,d,e,f,g){var h=function(b,f){g.get_string("templateselected","tool_templatelibrary",b).done(function(b){a('[data-region="displaytemplateheader"]').text(b)}).fail(d.exception);var h=f.match(/{{!([\s\S]*?)}}/g),i=0;if(null!==h)for(i=0;i<h.length;i++){var j=h[i];if(""!==j.trim()&&-1===j.indexOf("GNU General Public License")){j=j.substr(3,j.length-5),f=j;break}}a('[data-region="displaytemplatesource"]').text(f);var k=f.match(/Example context \(json\):([\s\S]*)/),l=!1;if(k){var m=k[1].trim();try{l=a.parseJSON(m)}catch(n){c.debug("Could not parse json example context for template."),c.debug(n)}}l?e.render(b,l).done(function(b,c){a('[data-region="displaytemplateexample"]').empty(),a('[data-region="displaytemplateexample"]').append(b),e.runTemplateJS(c)}).fail(d.exception):g.get_string("templatehasnoexample","tool_templatelibrary").done(function(b){a('[data-region="displaytemplateexample"]').text(b)}).fail(d.exception)},i=function(a){var c=a.split("/"),e=c.shift(),g=c.shift();b.call([{methodname:"core_output_load_template",args:{component:e,template:g,themename:f.theme},done:function(b){h(a,b)},fail:d.exception}])};return a('[data-region="list-templates"]').on("click","[data-templatename]",function(){var b=a(this).data("templatename");i(b)}),{}});

View File

@ -0,0 +1 @@
define(["jquery","core/ajax","core/log","core/notification","core/templates"],function(a,b,c,d,e){var f=function(b){e.render("tool_templatelibrary/search_results",{templates:b}).done(function(b){a('[data-region="searchresults"]').replaceWith(b)}).fail(d.exception)},g=function(){var c=a('[data-field="component"]').val(),e=a('[data-field="search"]').val();b.call([{methodname:"tool_templatelibrary_list_templates",args:{component:c,search:e},done:f,fail:d.exception}])},h=null,i=function(a,b){null!==h&&window.clearTimeout(h),h=window.setTimeout(function(){a(),h=null},b)},j=function(){i(g,400)};return a('[data-region="list-templates"]').on("change",'[data-field="component"]',j),a('[data-region="list-templates"]').on("input",'[data-field="search"]',j),g(),{}});

View File

@ -0,0 +1,114 @@
// 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/>.
/**
* This module adds ajax display functions to the template library page.
*
* @module tool_templatelibrary/display
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates', 'core/config', 'core/str'],
function($, ajax, log, notification, templates, config, str) {
/**
* Handle a template loaded response.
*
* @param {String} templateName The template name
* @param {String} source The template source
*/
var templateLoaded = function(templateName, source) {
str.get_string('templateselected', 'tool_templatelibrary', templateName).done(function(s) {
$('[data-region="displaytemplateheader"]').text(s);
}).fail(notification.exception);
// Remove the GPL from the start of the template.
var sections = source.match(/{{!([\s\S]*?)}}/g);
var i = 0;
// Find the first non-empty comment that is not the GPL.
// If no sections match - show the entire file.
if (sections !== null) {
for (i = 0; i < sections.length; i++) {
var section = sections[i];
if ((section.trim() !== '') && (section.indexOf('GNU General Public License') === -1)) {
// Remove {{! and }} from start and end.
section = section.substr(3, section.length - 5);
source = section;
break;
}
}
}
$('[data-region="displaytemplatesource"]').text(source);
// Now search the text for a json example.
var example = source.match(/Example context \(json\):([\s\S]*)/);
var context = false;
if (example) {
var rawJSON = example[1].trim();
try {
context = $.parseJSON(rawJSON);
} catch (e) {
log.debug('Could not parse json example context for template.');
log.debug(e);
}
}
if (context) {
templates.render(templateName, context).done(function(html, js) {
$('[data-region="displaytemplateexample"]').empty();
$('[data-region="displaytemplateexample"]').append(html);
templates.runTemplateJS(js);
}).fail(notification.exception);
} else {
str.get_string('templatehasnoexample', 'tool_templatelibrary').done(function(s) {
$('[data-region="displaytemplateexample"]').text(s);
}).fail(notification.exception);
}
};
/**
* Load the a template source from Moodle.
* @param {String} templateName
*/
var loadTemplate = function(templateName) {
var parts = templateName.split('/');
var component = parts.shift();
var name = parts.shift();
ajax.call([{
methodname: 'core_output_load_template',
args:{
component: component,
template: name,
themename: config.theme
},
done: function(source) { templateLoaded(templateName, source); },
fail: notification.exception
}]);
};
// Add the event listeners.
$('[data-region="list-templates"]').on('click', '[data-templatename]', function() {
var templatename = $(this).data('templatename');
loadTemplate(templatename);
});
// This module does not expose anything.
return {};
});

View File

@ -0,0 +1,89 @@
// 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/>.
/**
* This module adds ajax search functions to the template library page.
*
* @module tool_templatelibrary/search
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'],
function($, ajax, log, notification, templates) {
/**
* The ajax call has returned with a new list of templates.
*
* @method reloadListTemplate
* @param String[] templates List of template ids.
*/
var reloadListTemplate = function(templateList) {
templates.render('tool_templatelibrary/search_results', { templates: templateList })
.done(function (result) {
$('[data-region="searchresults"]').replaceWith(result);
}).fail(notification.exception);
};
/**
* Get the current values for the form inputs and refresh the list of matching templates.
*
* @method refreshSearch
*/
var refreshSearch = function() {
var componentStr = $('[data-field="component"]').val();
var searchStr = $('[data-field="search"]').val();
// Trigger the search.
ajax.call([
{ methodname: 'tool_templatelibrary_list_templates',
args: { component: componentStr, search: searchStr },
done: reloadListTemplate,
fail: notification.exception }
]);
};
var throttle = null;
/**
* Call the specified function after a delay. If this function is called again before the function is executed,
* the function will only be executed once.
*
* @method queueRefresh
* @param function callback
* @param int delay The time in milliseconds to delay.
*/
var queueRefresh = function(callback, delay) {
if (throttle !== null) {
window.clearTimeout(throttle);
}
throttle = window.setTimeout(function() {
callback();
throttle = null;
}, delay);
};
var changeHandler = function() {
queueRefresh(refreshSearch, 400);
};
// Add change handlers to refresh the list.
$('[data-region="list-templates"]').on('change', '[data-field="component"]', changeHandler);
$('[data-region="list-templates"]').on('input', '[data-field="search"]', changeHandler);
refreshSearch();
return {};
});

View File

@ -0,0 +1,92 @@
<?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/>.
/**
* Class for listing mustache templates.
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_templatelibrary;
use stdClass;
use core_component;
use coding_exception;
use required_capability_exception;
/**
* API exposed by tool_templatelibrary
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api {
/**
* Return a list of details about installed templates.
*
* @param string $component Filter the list to a single component.
* @param string $search Search string to optionally filter the list of templates.
* @return array[string] Where each template is in the form "component/templatename".
*/
public static function list_templates($component = '', $search = '') {
global $CFG;
$templatedirs = array();
$results = array();
if ($component != '') {
// Just look at one component for templates.
$dir = core_component::get_component_directory($component);
if (!$dir) {
return $templatedirs;
}
$templatedirs[$component] = $dir . '/templates';
} else {
// Look at all the templates dirs for all installed plugins.
$dir = $CFG->libdir . '/templates';
if (!empty($dir) && is_dir($dir)) {
$templatedirs['core'] = $dir;
}
$plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $dir) {
$plugins = core_component::get_plugin_list_with_file($type, 'templates', false);
foreach ($plugins as $plugin => $dir) {
if (!empty($dir) && is_dir($dir)) {
$templatedirs[$type . '_' . $plugin] = $dir;
}
}
}
}
foreach ($templatedirs as $templatecomponent => $dir) {
// List it.
$files = glob($dir . '/*.mustache');
foreach ($files as $file) {
$templatename = basename($file, '.mustache');
if ($search == '' || strpos($templatename, $search) !== false) {
$results[] = $templatecomponent . '/' . $templatename;
}
}
}
return $results;
}
}

View File

@ -0,0 +1,98 @@
<?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/>.
/**
* This is the external API for this tool.
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_templatelibrary;
require_once("$CFG->libdir/externallib.php");
use external_api;
use external_function_parameters;
use external_value;
use external_format_value;
use external_single_structure;
use external_multiple_structure;
use invalid_parameter_exception;
/**
* This is the external API for this tool.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external extends external_api {
/**
* Returns description of list_templates() parameters.
*
* @return external_function_parameters
*/
public static function list_templates_parameters() {
$component = new external_value(
PARAM_COMPONENT,
'The component to search',
VALUE_DEFAULT,
''
);
$search = new external_value(
PARAM_RAW,
'The search string',
VALUE_DEFAULT,
''
);
$params = array('component' => $component, 'search' => $search);
return new external_function_parameters($params);
}
/**
* Expose to AJAX
* @return boolean
*/
public static function list_templates_is_allowed_from_ajax() {
return true;
}
/**
* Loads the list of templates.
* @param string $component Limit the search to a component.
* @param string $search The search string.
* @return array[string]
*/
public static function list_templates($component, $search) {
$params = self::validate_parameters(self::list_templates_parameters(),
array(
'component' => $component,
'search' => $search,
));
return api::list_templates($component, $search);
}
/**
* Returns description of list_templates() result value.
*
* @return external_description
*/
public static function list_templates_returns() {
return new external_multiple_structure(new external_value(PARAM_RAW, 'The template name (format is component/templatename)'));
}
}

View File

@ -0,0 +1,72 @@
<?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/>.
/**
* Class containing data for list_templates page
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_templatelibrary\output;
use renderable;
use templatable;
use renderer_base;
use stdClass;
use core_plugin_manager;
use tool_templatelibrary\api;
/**
* Class containing data for list_templates page
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class list_templates_page implements renderable, templatable {
/**
* Export this data so it can be used as the context for a mustache template.
*
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$data->allcomponents = array();
$fulltemplatenames = api::list_templates();
$pluginmanager = core_plugin_manager::instance();
$components = array();
foreach ($fulltemplatenames as $templatename) {
list($component, $templatename) = explode('/', $templatename, 2);
$components[$component] = 1;
}
$components = array_keys($components);
foreach ($components as $component) {
$info = new stdClass();
$info->component = $component;
if ($component == 'core') {
$info->name = get_string('core_component', 'tool_templatelibrary');
} else {
$info->name = $pluginmanager->plugin_name($component);
}
$data->allcomponents[] = $info;
}
return $data;
}
}

View File

@ -0,0 +1,51 @@
<?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/>.
/**
* Renderer class for template library.
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_templatelibrary\output;
defined('MOODLE_INTERNAL') || die;
use plugin_renderer_base;
/**
* Renderer class for template library.
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {
/**
* Defer to template.
*
* @param list_templates_page $page
*
* @return string html for the page
*/
public function render_list_templates_page($page) {
$data = $page->export_for_template($this);
return parent::render_from_template('tool_templatelibrary/list_templates_page', $data);
}
}

View File

@ -0,0 +1,37 @@
<?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/>.
/**
* Template library webservice definitions.
*
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$functions = array(
'tool_templatelibrary_list_templates' => array(
'classname' => 'tool_templatelibrary\external',
'methodname' => 'list_templates',
'classpath' => '',
'description' => 'List/search templates by component.',
'type' => 'read',
'capabilities'=> '',
),
);

View File

@ -0,0 +1,47 @@
<?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/>.
/**
* This page lets users to manage site wide competencies.
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
admin_externalpage_setup('tooltemplatelibrary');
$component = optional_param('component', '', PARAM_COMPONENT);
$search = optional_param('search', '', PARAM_RAW);
$title = get_string('templates', 'tool_templatelibrary');
$pagetitle = get_string('searchtemplates', 'tool_templatelibrary');
// Set up the page.
$url = new moodle_url("/admin/tool/templatelibrary/index.php", array('component' => $component, 'search' => $search));
$PAGE->set_url($url);
$PAGE->set_title($title);
$PAGE->set_heading($title);
$output = $PAGE->get_renderer('tool_templatelibrary');
echo $output->header();
echo $output->heading($pagetitle);
$page = new \tool_templatelibrary\output\list_templates_page($component, $search);
echo $output->render($page);
echo $output->footer();

View File

@ -0,0 +1,37 @@
<?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/>.
/**
* Strings for component 'tool_templatelibrary', language 'en'
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['all'] = 'All components';
$string['component'] = 'Component';
$string['core_component'] = 'Moodle core';
$string['documentation'] = 'Documentation';
$string['example'] = 'Example';
$string['noresults'] = 'No results';
$string['notemplateselected'] = 'No template selected';
$string['pluginname'] = 'Template library';
$string['search'] = 'Search';
$string['searchtemplates'] = 'Search templates';
$string['templatehasnoexample'] = 'This template has no example context, so it cannot be rendered here. To add an example context to this template, insert in a Mustache comment "Example context (json):", followed by the json encoded sample context for the template.';
$string['templates'] = 'Templates';
$string['templateselected'] = 'Template: {$a}';

View File

@ -0,0 +1,33 @@
<?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/>.
/**
* Links and settings
*
* This file contains links and settings used by tool_templatelibrary
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
// Manage competency frameworks page.
$temp = new admin_externalpage(
'tooltemplatelibrary',
get_string('pluginname', 'tool_templatelibrary'),
new moodle_url('/admin/tool/templatelibrary/index.php')
);
$ADMIN->add('development', $temp);

View File

@ -0,0 +1,5 @@
[data-region="displaytemplateexample"] {
border-radius: 4px;
border: 1px inset #e3e3e3;
padding: 1em;
}

View File

@ -0,0 +1,48 @@
{{!
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/>.
}}
{{!
Moodle template to display another template.
The purpose of this template is to put scafolding in the page, so the javascript can fetch templates and
insert them into this part of the page.
Classes required for JS:
* none
Data attributes required for JS:
* data-region
Context variables required for this template:
* none
Example context (json):
{ }
}}
<div data-region="displaytemplate">
<h3 data-region="displaytemplateheader">{{#str}}notemplateselected, tool_templatelibrary{{/str}}</h3>
<div>
<h4>{{#str}}example, tool_templatelibrary{{/str}}</h4>
<div data-region="displaytemplateexample">
-
</div>
</div>
<div>
<h4>{{#str}}documentation, tool_templatelibrary{{/str}}</h4>
<pre data-region="displaytemplatesource"> - </pre>
</div>
</div>

View File

@ -0,0 +1,61 @@
{{!
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/>.
}}
{{!
Moodle template to the template library
The purpose of this template is build the entire page for the template library (by including smaller templates).
Classes required for JS:
* none
Data attributes required for JS:
* data-region, data-field
Context variables required for this template:
* allcomponents - array of components containing templates. Each component has a name and a component attribute.
}}
<div data-region="list-templates">
<form class="form-horizontal form-search">
<div class="control-group">
<label for="selectcomponent" class="control-label">{{#str}}component, tool_templatelibrary{{/str}}</label>
<div class="controls">
<select id="selectcomponent" data-field="component">
<option value="">{{#str}}all, tool_templatelibrary{{/str}}</option>
{{#allcomponents}}
<option value="{{component}}">{{name}}</option>
{{/allcomponents}}
</select>
</div>
</div>
<div class="control-group">
<label for="search" class="control-label">{{#str}}search, tool_templatelibrary{{/str}}</label>
<div class="controls">
<input type="text" id="search" class="search-query" data-field="search"/>
</div>
</div>
</form>
<hr/>
{{> tool_templatelibrary/search_results }}
<hr/>
{{> tool_templatelibrary/display_template }}
</div>
{{#js}}
require(['tool_templatelibrary/search', 'tool_templatelibrary/display']);
{{/js}}

View File

@ -0,0 +1,44 @@
{{!
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/>.
}}
{{!
Moodle template to display results of template search.
This template gets rendered by javascript when it has searched for templates.
Classes required for JS:
* none
Data attributes required for JS:
* data-region, data-templatename
Context variables required for this template:
* templates - And array of template names.
Example context (json):
{ "templates" : [ "core/pix_icon", "tool_templatelibrary/display_template" ] }
}}
<div data-region="searchresults" aria-live="off" class="no-overflow" style="max-height: 10em">
{{^templates}}
<p class="text-warning">{{#str}}noresults, tool_templatelibrary{{/str}}</p>
{{/templates}}
<ul>
{{#templates}}
<li data-templatename="{{.}}"><a href="#">{{.}}</a></li>
{{/templates}}
</ul>
</div>

View File

@ -0,0 +1,69 @@
<?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/>.
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use tool_templatelibrary\external;
/**
* External learning plans webservice API tests.
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_templatelibrary_external_testcase extends externallib_advanced_testcase {
/**
* Test list all.
*/
public function test_list_templates() {
$result = external::list_templates('', '');
$count = count($result);
// We have 3 templates in this tool - and there must be more else where.
$this->assertGreaterThan(3, $count);
}
/**
* Test we can filter by component.
*/
public function test_list_templates_for_component() {
$result = external::list_templates('tool_templatelibrary', '');
$count = count($result);
$this->assertEquals(3, $count);
$this->assertContains("tool_templatelibrary/display_template", $result);
$this->assertContains("tool_templatelibrary/search_results", $result);
$this->assertContains("tool_templatelibrary/list_templates_page", $result);
}
/**
* Test we can filter by a string.
*/
public function test_list_templates_with_filter() {
$result = external::list_templates('tool_templatelibrary', 'page');
$count = count($result);
// Should be only one matching template.
$this->assertEquals(1, $count);
$this->assertEquals($result[0], "tool_templatelibrary/list_templates_page");
}
}

View File

@ -0,0 +1,26 @@
<?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/>.
/**
* Plugin version info
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015021623; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2014110400; // Requires this Moodle version.
$plugin->component = 'tool_templatelibrary'; // Full name of the plugin (used for diagnostics).

View File

@ -1143,7 +1143,7 @@ class core_plugin_manager {
'assignmentupgrade', 'availabilityconditions', 'behat', 'capability', 'customlang', 'assignmentupgrade', 'availabilityconditions', 'behat', 'capability', 'customlang',
'dbtransfer', 'filetypes', 'generator', 'health', 'innodb', 'installaddon', 'dbtransfer', 'filetypes', 'generator', 'health', 'innodb', 'installaddon',
'langimport', 'log', 'messageinbound', 'multilangupgrade', 'monitor', 'phpunit', 'profiling', 'langimport', 'log', 'messageinbound', 'multilangupgrade', 'monitor', 'phpunit', 'profiling',
'replace', 'spamcleaner', 'task', 'replace', 'spamcleaner', 'task', 'templatelibrary',
'unittest', 'uploadcourse', 'uploaduser', 'unsuproles', 'xmldb' 'unittest', 'uploadcourse', 'uploaduser', 'unsuproles', 'xmldb'
), ),

View File

@ -27,5 +27,14 @@
Context variables required for this template: Context variables required for this template:
* attributes Array of name / value pairs. * attributes Array of name / value pairs.
Example context (json):
{
"attributes": [
{ "name": "src", "value": "http://moodle.com/wp-content/themes/moodle/images/logo-hat2.png" },
{ "name": "class", "value": "iconsmall" }
]
}
}} }}
<img {{#attributes}}{{name}}="{{value}}" {{/attributes}}/> <img {{#attributes}}{{name}}="{{value}}" {{/attributes}}/>