/** * {@inheritDoc} */ public function register(Application $app) { $app['mustache.options'] = array(); $app['mustache'] = $app->share(function ($app) { $defaults = array('loader' => $app['mustache.loader'], 'partials_loader' => $app['mustache.partials_loader'], 'helpers' => $app['mustache.helpers'], 'charset' => $app['charset']); if (isset($app['logger'])) { $defaults['logger'] = $app['logger']; } $app['mustache.options'] = array_replace($defaults, $app['mustache.options']); return new \Mustache_Engine($app['mustache.options']); }); $app['mustache.loader'] = $app->share(function ($app) { if (!isset($app['mustache.paths'])) { return new \Mustache_Loader_StringLoader(); } if (!is_array($app['mustache.paths'])) { return new \Mustache_Loader_FilesystemLoader($app['mustache.paths']); } $loader = new \Mustache_Loader_CascadingLoader(); foreach ($app['mustache.paths'] as $path) { $pathLoader = new \Mustache_Loader_FilesystemLoader($path); $loader->addLoader($pathLoader); } return $loader; }); $app['mustache.partials_loader'] = $app->share(function ($app) { if (isset($app['mustache.partials_path'])) { return new \Mustache_Loader_FilesystemLoader($app['mustache.partials_path']); } elseif (isset($app['mustache.partials'])) { return new \Mustache_Loader_ArrayLoader($app['mustache.partials']); } else { return $app['mustache.loader']; } }); $app['mustache.helpers'] = array(); }
/** * @expectedException Mustache_Exception_UnknownTemplateException */ public function testMissingTemplatesThrowExceptions() { $loader = new Mustache_Loader_CascadingLoader(array(new Mustache_Loader_ArrayLoader(array('foo' => '{{ foo }}')), new Mustache_Loader_ArrayLoader(array('bar' => '{{#bar}}BAR{{/bar}}')))); $loader->load('not_a_real_template'); }
/** * Return an instance of the mustache class. * * @since 2.9 * @return Mustache_Engine */ protected function get_mustache() { global $CFG; if ($this->mustache === null) { require_once $CFG->dirroot . '/lib/mustache/src/Mustache/Autoloader.php'; Mustache_Autoloader::register(); $themename = $this->page->theme->name; $themerev = theme_get_revision(); $target = $this->target; $cachedir = make_localcache_directory("mustache/{$themerev}/{$themename}/{$target}"); $loaderoptions = array(); // Where are all the places we should look for templates? $suffix = $this->component; if ($this->subtype !== null) { $suffix .= '_' . $this->subtype; } // Start with an empty list. $loader = new Mustache_Loader_CascadingLoader(array()); $loaderdir = $CFG->dirroot . '/theme/' . $themename . '/templates/' . $suffix; if (is_dir($loaderdir)) { $loader->addLoader(new \core\output\mustache_filesystem_loader($loaderdir, $loaderoptions)); } // Search each of the parent themes second. foreach ($this->page->theme->parents as $parent) { $loaderdir = $CFG->dirroot . '/theme/' . $parent . '/templates/' . $suffix; if (is_dir($loaderdir)) { $loader->addLoader(new \core\output\mustache_filesystem_loader($loaderdir, $loaderoptions)); } } // Look in a components templates dir for a base implementation. $compdirectory = core_component::get_component_directory($suffix); if ($compdirectory) { $loaderdir = $compdirectory . '/templates'; if (is_dir($loaderdir)) { $loader->addLoader(new \core\output\mustache_filesystem_loader($loaderdir, $loaderoptions)); } } // Look in the core templates dir as a final fallback. $compdirectory = $CFG->libdir; if ($compdirectory) { $loaderdir = $compdirectory . '/templates'; if (is_dir($loaderdir)) { $loader->addLoader(new \core\output\mustache_filesystem_loader($loaderdir, $loaderoptions)); } } $stringhelper = new \core\output\mustache_string_helper(); $jshelper = new \core\output\mustache_javascript_helper($this->page->requires); $pixhelper = new \core\output\mustache_pix_helper($this); // We only expose the variables that are exposed to JS templates. $safeconfig = $this->page->requires->get_config_for_javascript($this->page, $this); $helpers = array('config' => $safeconfig, 'str' => array($stringhelper, 'str'), 'js' => array($jshelper, 'help'), 'pix' => array($pixhelper, 'pix')); $this->mustache = new Mustache_Engine(array('cache' => $cachedir, 'escape' => 's', 'loader' => $loader, 'helpers' => $helpers)); } return $this->mustache; }