public static function list_templates($component = '', $search = '', $themename = '') {
        global $CFG, $PAGE;

        if (empty($themename)) {
            $themename = $PAGE->theme->name;
        }
        $themeconfig = \theme_config::load($themename);

        $templatedirs = array();
        $results = array();

        if ($component !== '') {
            // Just look at one component for templates.
            $dirs = mustache_template_finder::get_template_directories_for_component($component, $themename);

            $templatedirs[$component] = $dirs;
        } else {

            // Look at all the templates dirs for core.
            $templatedirs['core'] = mustache_template_finder::get_template_directories_for_component('core', $themename);

            // Look at all the templates dirs for subsystems.
            $subsystems = core_component::get_core_subsystems();
            foreach ($subsystems as $subsystem => $dir) {
                if (empty($dir)) {
                    continue;
                }
                $dir .= '/templates';
                if (is_dir($dir)) {
                    $dirs = mustache_template_finder::get_template_directories_for_component('core_' . $subsystem, $themename);
                    $templatedirs['core_' . $subsystem] = $dirs;
                }
            }

            // Look at all the templates dirs for plugins.
            $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 ($type == 'theme' && $plugin != $themename && !in_array($plugin, $themeconfig->parents)) {
                        continue;
                    }
                    if (!empty($dir) && is_dir($dir)) {
                        $pluginname = $type . '_' . $plugin;
                        $dirs = mustache_template_finder::get_template_directories_for_component($pluginname, $themename);
                        $templatedirs[$pluginname] = $dirs;
                    }
                }
            }
        }

        foreach ($templatedirs as $templatecomponent => $dirs) {
            foreach ($dirs as $dir) {
                // List it.
                $files = glob($dir . '/*.mustache');

                foreach ($files as $file) {
                    $templatename = basename($file, '.mustache');
                    if ($search == '' || strpos($templatename, $search) !== false) {
                        $results[$templatecomponent . '/' . $templatename] = 1;
                    }
                }
            }
        }
        $results = array_keys($results);
        sort($results);
        return $results;
    }