function __construct() { // This is how to call the template engine: // do_action('wunderground_render_template', $file_name, $data_array ); add_action('wunderground_render_template', array(&$this, 'render'), 10, 2); // Set up Twig Twig_Autoloader::register(); // This path should always be the last $base_path = trailingslashit(plugin_dir_path(Wunderground_Plugin::$file)) . 'templates'; $this->loader = new Twig_Loader_Filesystem($base_path); // Tap in here to add additional template paths $additional_paths = apply_filters('wunderground_template_paths', array(trailingslashit(get_stylesheet_directory()) . 'wunderground')); foreach ($additional_paths as $path) { // If the directory exists if (is_dir($path)) { // Tell Twig to use it first $this->loader->prependPath($path); } } // You can force debug mode by adding `add_filter( 'wunderground_twig_debug' '__return_true' );` $debug = apply_filters('wunderground_twig_debug', current_user_can('manage_options') && isset($_GET['debug'])); $this->twig = new Twig_Environment($this->loader, array('debug' => !empty($debug), 'auto_reload' => true)); if (!empty($debug)) { $this->twig->addExtension(new Twig_Extension_Debug()); } }
/** * add a directory to template search dirs * @param string $directory * @param bool|true $primary * @return ITemplateEngine|void */ public function addDir($directory, $primary = true) { if ($primary) { $this->twigLoader->prependPath($directory); } else { $this->twigLoader->addPath($directory); } $this->twigEnv->setLoader($this->twigLoader); return $this; }
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')); }
/** * @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')); }
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')); }
/** * Add a templates directory to the beginning of the list of existing ones known by the current Twig templates loader. * @param string $path Path to the templates directory to add. * @param string $namespace Namespace used to reference the directory inside the Twig templates loader. */ public function prependTemplatesDirectory($path, $namespace = false) { if (StringUtils::emptyOrSpaces($namespace)) { $this->_loader->prependPath($path); } else { $this->_loader->prependPath($path, trim($namespace)); } }
public function register(Container $app) { $app['twig.options'] = array('autoescape' => false, 'charset' => $app['app.charset'], 'debug' => $app['app.debug'], 'strict_variables' => $app['app.debug']); $app['twig.loader'] = function () use($app) { $theme = ucfirst($app->edition('theme')); $format = Toolkit::camelize($app->edition('format'), true); $loader = new \Twig_Loader_Filesystem($app['app.dir.themes']); // Base theme (common styles per edition type) // <easybook>/app/Resources/Themes/Base/<edition-type>/Templates/<template-name>.twig $baseThemeDir = sprintf('%s/Base/%s/Templates', $app['app.dir.themes'], $format); $loader->addPath($baseThemeDir); $loader->addPath($baseThemeDir, 'theme'); $loader->addPath($baseThemeDir, 'theme_base'); // Book theme (configured per edition in 'config.yml') // <easybook>/app/Resources/Themes/<theme>/<edition-type>/Templates/<template-name>.twig $bookThemeDir = sprintf('%s/%s/%s/Templates', $app['app.dir.themes'], $theme, $format); $loader->prependPath($bookThemeDir); $loader->prependPath($bookThemeDir, 'theme'); $userTemplatePaths = array($app['publishing.dir.templates'], sprintf('%s/%s', $app['publishing.dir.templates'], strtolower($format)), sprintf('%s/%s', $app['publishing.dir.templates'], $app['publishing.edition'])); foreach ($userTemplatePaths as $path) { if (file_exists($path)) { $loader->prependPath($path); } } $defaultContentPaths = array(sprintf('%s/Base/%s/Contents', $app['app.dir.themes'], $format), sprintf('%s/%s/%s/Contents', $app['app.dir.themes'], $theme, $format)); foreach ($defaultContentPaths as $path) { if (file_exists($path)) { $loader->prependPath($path, 'content'); } } return $loader; }; $app['twig'] = function () use($app) { $twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']); $twig->addExtension(new TwigCssExtension()); $twig->addGlobal('app', $app); if (null !== ($bookConfig = $app['publishing.book.config'])) { $twig->addGlobal('book', $bookConfig['book']); $publishingEdition = $app['publishing.edition']; $editions = $app->book('editions'); $twig->addGlobal('edition', $editions[$publishingEdition]); } return $twig; }; }
private function configureLoader(\Twig_Loader_Filesystem $loader, string $profile) { if (null === ($dancerDir = $this->config->get('dancer_directory'))) { return; } if (is_dir($dancerDir . '/templates')) { $loader->prependPath($dancerDir . '/templates'); } $profile = str_replace(':', '_', $profile); if ('' !== $profile && is_dir($dancerDir . '/templates/' . $profile)) { $loader->addPath($dancerDir . '/templates/' . $profile); } }
public function createTemplateLoader($templatePath, $themePath = null) { $settings = $this->application->settings; if (array_key_exists('template_loader', $settings)) { return $settings['template_loader']; } $args = []; $fileLoader = new \Twig_Loader_Filesystem($templatePath); if ($themePath) { if (isset($this->theme)) { $fileLoader->prependPath($themePath . DS . $this->theme . DS . 'templates' . DS); } } $templates = $this->application->templates; if ($templates) { foreach ($templates as $name => $templatePath) { $fileLoader->addPath($templatePath, $name); } } $loaders = [$fileLoader]; $loader = new \Twig_Loader_Chain($loaders); $options = []; if (array_key_exists('template_options', $settings)) { $options = $settings['template_options']; } $twig = new \Twig_Environment($loader, $options); foreach ($this->ui['modules'] as $uiModule) { $twig->addExtension(new $uiModule($this)); } return $twig; }
private function _initTwig() { require_once BASE_FOLDER . '/lib/Twig/Autoloader.php'; Twig_Autoloader::register(); // file loader $loader = new Twig_Loader_Filesystem(TEMPLATES_FOLDER . '/' . TEMPLATE_SUBDIR_DEFAULT); $skinSubDir = $this->_skin->getTemplatesSubDirectory(); if (strlen($skinSubDir) && $skinSubDir != TEMPLATE_SUBDIR_DEFAULT) { $loader->prependPath(TEMPLATES_FOLDER . '/' . $skinSubDir); } // environment config $twigConfig = array('cache' => CACHE_FOLDER); if (DEBUG) { $twigConfig['auto_reload'] = TRUE; $twigConfig['strict_variables'] = TRUE; } // init $this->_environment = new Twig_Environment($loader, $twigConfig); }