/** * @see AbstractTheme::setTwigLoaderPaths() * * @param \Twig_Loader_Filesystem $loader */ protected function setTwigLoaderPaths(\Twig_Loader_Filesystem $loader) { $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); }
public function renderer() { if (!$this->renderer) { $gantry = \Gantry\Framework\Gantry::instance(); /** @var UniformResourceLocator $locator */ $locator = $gantry['locator']; $loader = new \Twig_Loader_Filesystem($locator->findResources('gantry-engine://twig')); $loader->setPaths($locator->findResources('gantry-pages://'), 'pages'); $loader->setPaths($locator->findResources('gantry-positions://'), 'positions'); $params = array('cache' => $locator('gantry-cache://twig', true, true), 'debug' => true, 'auto_reload' => true, 'autoescape' => 'html'); $twig = new \Twig_Environment($loader, $params); $this->add_to_twig($twig); $this->renderer = $twig; } return $this->renderer; }
protected function getTwig() { $options = ['cache' => false, 'strict_variables' => true]; $loader = new \Twig_Loader_Filesystem(); $loader->setPaths($this->skeletonDirs); return new \Twig_Environment($loader, $options); }
/** * This renders a file based on the $file and sf_type * * @param string $file the fullpath to the template file * @return string */ protected function renderFile($file) { if (sfConfig::get('sf_logging_enabled', false)) { $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s".', $file)))); } $this->loader->setPaths((array) realpath(dirname($file))); return $this->twig->loadTemplate(basename($file))->render($this->getAttributeHolderVariables()); }
/** * @see AbstractTheme::setTwigLoaderPaths() * * @param \Twig_Loader_Filesystem $loader */ protected function setTwigLoaderPaths(\Twig_Loader_Filesystem $loader) { $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'); }
/** * This renders a file based on the $file and sf_type. * * @param string $file the fullpath to the template file * * @return string */ protected function renderFile($file) { if (sfConfig::get('sf_logging_enabled', false)) { $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s"', $file)))); } $this->loader->setPaths((array) realpath(dirname($file))); $event = $this->dispatcher->filter(new sfEvent($this, 'template.filter_parameters'), $this->attributeHolder->getAll()); return $this->twig->loadTemplate(basename($file))->render($event->getReturnValue()); }
/** * @see AbstractTheme::setTwigLoaderPaths() * * @param \Twig_Loader_Filesystem $loader */ protected function setTwigLoaderPaths(\Twig_Loader_Filesystem $loader) { $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); }
public function setNamespaces(\Silex\Application $app) { $namespaces = $this->getNamespaces($app); $app['twig.loader.filesystem'] = $app->share(function ($app) use($namespaces) { $loader = new \Twig_Loader_Filesystem($app['twig.path']); foreach ($namespaces as $ns => $path) { $loader->setPaths($path, $ns); } return $loader; }); }
function load_twig() { global $twig, $config; require 'lib/Twig/Autoloader.php'; Twig_Autoloader::register(); Twig_Autoloader::autoload('Twig_Extensions_Node_Trans'); Twig_Autoloader::autoload('Twig_Extensions_TokenParser_Trans'); Twig_Autoloader::autoload('Twig_Extensions_Extension_I18n'); Twig_Autoloader::autoload('Twig_Extensions_Extension_Tinyboard'); $loader = new Twig_Loader_Filesystem($config['dir']['template']); $loader->setPaths($config['dir']['template']); $twig = new Twig_Environment($loader, array('autoescape' => false, 'cache' => is_writable('templates') && (!is_dir('templates/cache') || is_writable('templates/cache')) ? "{$config['dir']['template']}/cache" : false, 'debug' => $config['debug'])); $twig->addExtension(new Twig_Extensions_Extension_Tinyboard()); $twig->addExtension(new Twig_Extensions_Extension_I18n()); }
public function testPaths() { $basePath = dirname(__FILE__) . '/Fixtures'; $loader = new Twig_Loader_Filesystem(array($basePath . '/normal', $basePath . '/normal_bis')); $loader->setPaths(array($basePath . '/named', $basePath . '/named_bis'), 'named'); $loader->addPath($basePath . '/named_ter', 'named'); $loader->addPath($basePath . '/normal_ter'); $loader->prependPath($basePath . '/normal_final'); $loader->prependPath($basePath . '/named_final', 'named'); $this->assertEquals(array($basePath . '/normal_final', $basePath . '/normal', $basePath . '/normal_bis', $basePath . '/normal_ter'), $loader->getPaths()); $this->assertEquals(array($basePath . '/named_final', $basePath . '/named', $basePath . '/named_bis', $basePath . '/named_ter'), $loader->getPaths('named')); $this->assertEquals("path (final)\n", $loader->getSource('index.html')); $this->assertEquals("path (final)\n", $loader->getSource('@__main__/index.html')); $this->assertEquals("named path (final)\n", $loader->getSource('@named/index.html')); }
public function testPaths() { $basePath = __DIR__ . '/Fixtures'; $loader = new Twig_Loader_Filesystem(array($basePath . '/normal', $basePath . '/normal_bis')); $loader->setPaths(array($basePath . '/named', $basePath . '/named_bis'), 'named'); $loader->addPath($basePath . '/named_ter', 'named'); $loader->addPath($basePath . '/normal_ter'); $loader->prependPath($basePath . '/normal_final'); $loader->prependPath($basePath . '/named/../named_quater', 'named'); $loader->prependPath($basePath . '/named_final', 'named'); $this->assertEquals(array($basePath . '/normal_final', $basePath . '/normal', $basePath . '/normal_bis', $basePath . '/normal_ter'), $loader->getPaths()); $this->assertEquals(array($basePath . '/named_final', $basePath . '/named/../named_quater', $basePath . '/named', $basePath . '/named_bis', $basePath . '/named_ter'), $loader->getPaths('named')); $this->assertEquals(realpath($basePath . '/named_quater/named_absolute.html'), realpath($loader->getCacheKey('@named/named_absolute.html'))); $this->assertEquals("path (final)\n", $loader->getSource('index.html')); $this->assertEquals("path (final)\n", $loader->getSource('@__main__/index.html')); $this->assertEquals("named path (final)\n", $loader->getSource('@named/index.html')); }
/** * @dataProvider getBasePaths */ public function testPaths($basePath) { $loader = new Twig_Loader_Filesystem(array($basePath . '/normal', $basePath . '/normal_bis')); $loader->setPaths(array($basePath . '/named', $basePath . '/named_bis'), 'named'); $loader->addPath($basePath . '/named_ter', 'named'); $loader->addPath($basePath . '/normal_ter'); $loader->prependPath($basePath . '/normal_final'); $loader->prependPath($basePath . '/named/../named_quater', 'named'); $loader->prependPath($basePath . '/named_final', 'named'); $this->assertEquals(array($basePath . '/normal_final', $basePath . '/normal', $basePath . '/normal_bis', $basePath . '/normal_ter'), $loader->getPaths()); $this->assertEquals(array($basePath . '/named_final', $basePath . '/named/../named_quater', $basePath . '/named', $basePath . '/named_bis', $basePath . '/named_ter'), $loader->getPaths('named')); // do not use realpath here as it would make the test unuseful $this->assertEquals(str_replace('\\', '/', $basePath . '/named_quater/named_absolute.html'), str_replace('\\', '/', $loader->getCacheKey('@named/named_absolute.html'))); $this->assertEquals("path (final)\n", $loader->getSource('index.html')); $this->assertEquals("path (final)\n", $loader->getSource('@__main__/index.html')); $this->assertEquals("named path (final)\n", $loader->getSource('@named/index.html')); }
/** * Sets the paths where templates are stored. * * @param string|array $paths A path or an array of paths where to look for templates. * @param bool $overrideBaseDir If true a path can be outside themes base directory. * * @return Twig * * @since 1.0 */ public function setTemplatesPaths($paths, $overrideBaseDir = false) { if (!is_array($paths)) { $paths = array($paths); } foreach ($paths as $path) { if ($overrideBaseDir) { $this->templatesPaths[] = $path; } else { $this->templatesPaths[] = $this->config['templates_base_dir'] . $path; } } // Reset the paths if needed. if (is_object($this->twigLoader)) { try { $this->twigLoader->setPaths($this->templatesPaths); } catch (\Twig_Error_Loader $e) { echo $e->getRawMessage(); } } return $this; }
public function setViewPaths(array $paths = array()) { $this->loader->setPaths($paths); }
public function selectTheme($theme) { $this->themeName = $theme; $this->selectThemePath(); $this->loader->setPaths($this->dir() . $this->themePath); }
/** * Add a selection of paths with the desired namespace * * @param array $paths * @return \Twig_Loader_Filesystem */ private function addPaths(array $paths) { $loader = new \Twig_Loader_Filesystem(); foreach ($paths as $namespace => $path) { if (is_string($namespace)) { $loader->setPaths($path, $namespace); } else { $loader->addPath($path); } } return $loader; }
/** * @param string $templateDir * @return void */ public function setScriptPath($templateDir) { $this->loader->setPaths($templateDir); }