/** * Constructs a TwigEnvironment object and stores cache and storage * internally. */ public function __construct(\Twig_LoaderInterface $loader = NULL, $options = array(), ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) { // @todo Pass as arguments from the DIC. $this->cache_object = \Drupal::cache(); // Ensure that twig.engine is loaded, given that it is needed to render a // template because functions like twig_drupal_escape_filter are called. require_once DRUPAL_ROOT . '/core/themes/engines/twig/twig.engine'; // Set twig path namespace for themes and modules. $namespaces = array(); foreach ($module_handler->getModuleList() as $name => $extension) { $namespaces[$name] = $extension->getPath(); } foreach ($theme_handler->listInfo() as $name => $extension) { $namespaces[$name] = $extension->getPath(); } foreach ($namespaces as $name => $path) { $templatesDirectory = $path . '/templates'; if (file_exists($templatesDirectory)) { $loader->addPath($templatesDirectory, $name); } } $this->templateClasses = array(); $this->stringLoader = new \Twig_Loader_String(); parent::__construct($loader, $options); }
/** * {@inheritdoc} */ public function isFresh($name, $time) { try { return filemtime($this->findTemplate($name)) <= $time; } catch (\Exception $exception) { return $this->decoratedLoader->isFresh($name, $time); } }
/** * {@inheritdoc} */ public function isFresh($name, $time) { $templates = $this->getTemplates($name); foreach ($templates as $template) { try { return $this->loader->isFresh($template, $time); } catch (\Twig_Error $e) { } } throw new \RuntimeException(sprintf("Template \"%s\" not found. Tried the following:\n%s", $name, implode("\n", $templates))); }
/** * @see AbstractTheme::setTwigLoaderPaths() * * @param \Twig_LoaderInterface $loader */ protected function setTwigLoaderPaths(\Twig_LoaderInterface $loader) { if (!$loader instanceof \Twig_Loader_Filesystem) { return; } $gantry = static::gantry(); /** @var UniformResourceLocator $locator */ $locator = $gantry['locator']; $loader->setPaths($locator->findResources('gantry-engine://twig')); $loader->setPaths($locator->findResources('gantry-pages://'), 'pages'); $loader->setPaths($locator->findResources('gantry-positions://'), 'positions'); parent::setTwigLoaderPaths($loader); }
/** * @see AbstractTheme::setTwigLoaderPaths() * * @param \Twig_LoaderInterface $loader */ protected function setTwigLoaderPaths(\Twig_LoaderInterface $loader) { if (!$loader instanceof \Twig_Loader_Filesystem) { return; } $gantry = static::gantry(); /** @var UniformResourceLocator $locator */ $locator = $gantry['locator']; $paths = $locator->mergeResources(['gantry-theme://templates', 'gantry-engine://templates']); // TODO: right now we are replacing all paths; we need to do better, but there are some issues with this call. $loader->setPaths($paths); parent::setTwigLoaderPaths($loader); }
/** * Add view path * * @param string $path * @param string $namespace * * @return \Bridge\Bundle\TwigBundle\TwigTemplating */ public function addPath($path, $namespace = \Twig_Loader_Filesystem::MAIN_NAMESPACE) { if ($this->loader instanceof \Twig_Loader_Filesystem) { if (is_dir($path)) { $this->loader->addPath($path, $namespace); } } return $this; }
public function __construct(Twig_LoaderInterface $loader) { $this->loader = $loader; $templates = array(); if ($loader instanceof Twig_Loader_Filesystem) { foreach ($loader->getNamespaces() as $namespace) { $paths = $loader->getPaths($namespace); foreach ($paths as $path) { foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($path)), RecursiveIteratorIterator::LEAVES_ONLY) as $file) { if (Twig_Loader_Filesystem::MAIN_NAMESPACE === $namespace) { $templates[] = substr($file->getPathname(), strlen($path) + 1); } else { $templates[] = '@' . $namespace . '/' . $file->getPathname(); } } } } } print_r($templates); }
/** * Constructs a TwigEnvironment object and stores cache and storage * internally. */ public function __construct(\Twig_LoaderInterface $loader = NULL, $options = array(), ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) { // @todo Pass as arguments from the DIC. $this->cache_object = \Drupal::cache(); // Set twig path namespace for themes and modules. $namespaces = array(); foreach ($module_handler->getModuleList() as $name => $extension) { $namespaces[$name] = $extension->getPath(); } foreach ($theme_handler->listInfo() as $name => $extension) { $namespaces[$name] = $extension->getPath(); } foreach ($namespaces as $name => $path) { $templatesDirectory = $path . '/templates'; if (file_exists($templatesDirectory)) { $loader->addPath($templatesDirectory, $name); } } $this->templateClasses = array(); parent::__construct($loader, $options); }
/** * Retorna una instancia de la interfaz Twig_LoaderInterface. * * @return Twig_LoaderInterface */ public static function getLoader() { if (is_null(self::$loader)) { self::$loader = new Twig_Loader_Filesystem(self::$config['templates_dir']); if (!is_null(self::$config['namespaces']) && is_array(self::$config['namespaces'])) { foreach (self::$config['namespaces'] as $namespace => $dir) { self::$loader->addPath($dir, $namespace); } } } return self::$loader; }
/** * {@inheritdoc} */ public function exists($name) { if ($this->loader instanceof \Twig_ExistsLoaderInterface) { return $this->loader->exists($name); } else { try { $this->loader->getSource($name); return true; } catch (\Twig_Error_Loader $e) { return false; } } }
/** * Collect asset resources defined in twig templates * * @param FilterResponseEvent $event */ public function onKernelResponse(FilterResponseEvent $event) { if (count($this->templates) === 0) { // Automatically find templates only if loader compatible if ($this->loader instanceof \Twig_Loader_Filesystem) { $twigNamespaces = $this->loader->getNamespaces(); foreach ($twigNamespaces as $ns) { if (count($this->loader->getPaths($ns)) > 0) { $iterator = Finder::create()->files()->in($this->loader->getPaths($ns)); /* @var $file \Symfony\Component\Finder\SplFileInfo */ foreach ($iterator as $file) { $resource = new TwigResource($this->loader, '@' . $ns . '/' . $file->getRelativePathname()); $this->manager->addResource($resource, 'twig'); } } } } } else { foreach ($this->templates as $template) { $resource = new TwigResource($this->loader, $template); $this->manager->addResource($resource, 'twig'); } } }
public function setLoader(Twig_LoaderInterface $loader) { $this->loader = $loader; $loader->setEnvironment($this); }
/** * @see AbstractTheme::setTwigLoaderPaths() * * @param \Twig_LoaderInterface $loader */ protected function setTwigLoaderPaths(\Twig_LoaderInterface $loader) { if (!$loader instanceof \Twig_Loader_Filesystem) { return; } $gantry = static::gantry(); /** @var UniformResourceLocator $locator */ $locator = $gantry['locator']; $loader->setPaths($locator->findResources('gantry-admin://templates')); $loader->setPaths($locator->findResources('gantry-admin://templates'), 'gantry-admin'); }
/** * Adds a folder to the loader. * * @param $folder */ public function addFolder($folder) { $this->loader->addPath($folder); }
public function addBasePath($path, $classPrefix = 'Zend_View') { $this->loader->addPath($path); }