protected function createTwigLoader(SettingsInterface $settings) { if (!$settings->has('template_paths')) { throw new RuntimeError('Missing "template_paths" settings with template lookup locations.'); } $template_paths = (array) $settings->get('template_paths', []); $loader = new FilesystemLoader($template_paths); if ($settings->has('allowed_template_extensions')) { $loader->setAllowedExtensions((array) $settings->get('allowed_template_extensions')); } if (!$settings->has('cache_scope')) { $loader->setScope(spl_object_hash($loader)); // unique scope for each new loader instance } else { $loader->setScope($settings->get('cache_scope', FilesystemLoader::SCOPE_DEFAULT)); } // adds an @namespaces to templates to allow twig templates to use embed/include statements that reuse // existing templates to override blocks instead of copying whole templates (from different locations) // usage example: {% include "@Honeybee/foo.twig" ignore if missing %} $loader->addPath(AgaviConfig::get('core.template_dir'), 'App'); $loader->addPath(AgaviConfig::get('core.honeybee_template_dir'), 'Honeybee'); foreach ($this->getModuleTemplatesPaths() as $module_name => $templates_path) { $loader->addPath($templates_path, $module_name); } return $loader; }
protected static function createTwigLoader(Settings $settings) { if (!$settings->has('template_paths')) { throw new RuntimeError('Missing "template_paths" settings with template lookup locations.'); } $template_paths = (array) $settings->get('template_paths', []); $loader = new FilesystemLoader($template_paths); if ($settings->has('allowed_template_extensions')) { $loader->setAllowedExtensions((array) $settings->get('allowed_template_extensions')); } if (!$settings->has('cache_scope')) { $loader->setScope(spl_object_hash($loader)); // unique scope for each new loader instance } else { $loader->setScope($settings->get('cache_scope', FilesystemLoader::SCOPE_DEFAULT)); } return $loader; }
protected function getSource(AgaviTemplateLayer $layer) { $twig = $this->getEngine(); $template_dirs = (array) $this->getParameter('template_dirs', array(AgaviConfig::get('core.template_dir'))); $path = $layer->getResourceStreamIdentifier(); if ($layer instanceof AgaviFileTemplateLayer) { $paths = array(); // allow loading from the main project template dir by default // (and any other directories the user has set through configuration) foreach ($template_dirs as $dir) { // replace e.g. {module} with name of the current module if possible $dir = AgaviToolkit::expandVariables($dir, array_merge(array_filter($layer->getParameters(), 'is_scalar'), array_filter($layer->getParameters(), 'is_null'))); if (is_dir($dir) && is_readable($dir)) { $paths[] = $dir; } else { //\AgaviContext::getInstance()->getLoggerManager()->logTo( //null, Logging\Logger::INFO, __METHOD__, "Template directory $dir does not exist or is not //readable. Check 'core.template_dir' setting or the TwigRenderer's 'template_dirs' parameter //or create the directory."); } } // set the directory the template is in as the first path to load from, and the directory set on // the layer second - that way, including another template inside this template will look at e.g. // a locale subdirectory first before falling back to the originally defined folder $pathinfo = pathinfo($path); $paths[] = $pathinfo['dirname']; $paths[] = $layer->getParameter('directory'); $loader = new FilesystemLoader($paths); $loader->setScope('views'); $loader->setAllowedExtensions($this->getParameter('allowed_template_extensions', array('.html', '.twig', '.haml'))); // adds namespaces to templates to allow twig templates to use embed/include statements that reuse // existing templates from specific locations (determined by their namespace prefix, e. g. @App ) $loader->addPath(AgaviConfig::get('core.template_dir'), 'App'); $loader->addPath(AgaviConfig::get('core.honeybee_template_dir'), 'Honeybee'); foreach ($this->modules as $module_name => $module_path) { $loader->addPath($module_path, $module_name); } $twig->setLoader($loader); $source = $pathinfo['basename']; } else { // a stream template or whatever; either way, it's something Twig can't load directly :S $twig->setLoader(new Twig_Loader_String()); $source = file_get_contents($path); } return $source; }