2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * List the valid locations to search for a template with a given name.
22 * @copyright 2015 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core\output
;
34 * Get information about valid locations for mustache templates.
36 * @copyright 2015 Damyon Wiese
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class mustache_template_finder
{
43 * Helper function for getting a list of valid template directories for a specific component.
45 * @param string $component The component to search
46 * @param string $themename The current theme name
47 * @return string[] List of valid directories for templates for this compoonent. Directories are not checked for existence.
49 public static function get_template_directories_for_component($component, $themename = '') {
53 if ($themename == '') {
54 $themename = $PAGE->theme
->name
;
57 // Clean params for safety.
58 $component = clean_param($component, PARAM_COMPONENT
);
59 $themename = clean_param($themename, PARAM_COMPONENT
);
61 // Validate the component.
63 $compdirectory = core_component
::get_component_directory($component);
64 if (!$compdirectory) {
65 throw new coding_exception("Component was not valid: " . s($component));
68 // Find the parent themes.
70 if ($themename === $PAGE->theme
->name
) {
71 $parents = $PAGE->theme
->parents
;
73 $themeconfig = theme_config
::load($themename);
74 $parents = $themeconfig->parents
;
77 // First check the theme.
78 $dirs[] = $CFG->dirroot
. '/theme/' . $themename . '/templates/' . $component . '/';
79 if (isset($CFG->themedir
)) {
80 $dirs[] = $CFG->themedir
. '/' . $themename . '/templates/' . $component . '/';
82 // Now check the parent themes.
83 // Search each of the parent themes second.
84 foreach ($parents as $parent) {
85 $dirs[] = $CFG->dirroot
. '/theme/' . $parent . '/templates/' . $component . '/';
86 if (isset($CFG->themedir
)) {
87 $dirs[] = $CFG->themedir
. '/' . $parent . '/templates/' . $component . '/';
91 $dirs[] = $compdirectory . '/templates/';
97 * Helper function for getting a filename for a template from the template name.
99 * @param string $name - This is the componentname/templatename combined.
100 * @param string $themename - This is the current theme name.
103 public static function get_template_filepath($name, $themename = '') {
106 if (strpos($name, '/') === false) {
107 throw new coding_exception('Templates names must be specified as "componentname/templatename"' .
108 ' (' . s($name) . ' requested) ');
110 list($component, $templatename) = explode('/', $name, 2);
111 $component = clean_param($component, PARAM_COMPONENT
);
112 if (strpos($templatename, '/') !== false) {
113 throw new coding_exception('Templates cannot be placed in sub directories (' . s($name) . ' requested)');
116 $dirs = self
::get_template_directories_for_component($component, $themename);
118 foreach ($dirs as $dir) {
119 $candidate = $dir . $templatename . '.mustache';
120 if (file_exists($candidate)) {
125 throw new moodle_exception('filenotfound', 'error', '', null, $name);