public function testGetNamespaces() { $loader = new Twig_Loader_Filesystem(sys_get_temp_dir()); $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE), $loader->getNamespaces()); $loader->addPath(sys_get_temp_dir(), 'named'); $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE, 'named'), $loader->getNamespaces()); }
protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('<comment>Iniciando busca de templates</comment>'); /** * @var $app \app\AppInit */ $app = $this->getHelper('init')->getInit(); $cache = $app->getCache(); $src = $app->getBaseDir() . "/src"; $loader = new \Twig_Loader_Filesystem($src); $twig = new \Twig_Environment($loader, array('cache' => $cache['twig'], 'auto_reload' => true)); $config = $app->config(); $container = $app->container(); foreach ($config['modules'] as $ap) { if (file_exists($container['Modules'] . $ap . '/Views')) { $loader->addPath($container['Modules'] . $ap . '/Views', $ap); } if (file_exists($container['Modules'] . $ap . '/Templates')) { $loader->addPath($container['Modules'] . $ap . '/Templates', $ap); } } $twig->addExtension(new \Twig_Extensions_Extension_I18n()); $d = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src), \RecursiveIteratorIterator::LEAVES_ONLY); $Regex2 = new \RegexIterator($d, '/\\.html.twig$/i'); foreach ($Regex2 as $file) { if ($file->isFile()) { $twig->loadTemplate(str_replace($src . '/', '', $file)); } } #$output->writeln("<comment>Finalizando</comment>"); }
public function initExtension($extension, \Twig_Environment $render, \Twig_Loader_Filesystem $fileSystemLoader) { if (false === strpos($extension, "\\")) { $extension = "\\" . $extension; } $config = $this->getConfig(); switch ($extension) { case "\\TranslationExtension": $config = isset($config["translationExtension"]) ? $config["translationExtension"] : []; $lang = isset($config["lang"]) ? $config["lang"] : "ru"; $locale = isset($config["locale"]) ? $config["locale"] : "ru_RU"; $translator = new \Symfony\Component\Translation\Translator($locale); $translator->addLoader('xlf', new \Symfony\Component\Translation\Loader\XliffFileLoader()); $vendorFormDir = VENDOR_DIR . '/symfony/form/Symfony/Component/Form'; $vendorValidatorDir = VENDOR_DIR . '/symfony/validator/Symfony/Component/Validator'; $translator->addResource('xlf', $vendorFormDir . "/Resources/translations/validators.{$lang}.xlf", $locale, 'validators'); $translator->addResource('xlf', $vendorValidatorDir . "/Resources/translations/validators.{$lang}.xlf", $locale, 'validators'); $extension = new \Symfony\Bridge\Twig\Extension\TranslationExtension($translator); break; case "\\FormExtension": $config = isset($config["formExtension"]) ? $config["formExtension"] : []; $templates = $config["templates"] ?: "/vendor/symfony/twig-bridge/Resources/views/Form"; $templates = $this->getRootDir() . "/" . $templates; $fileSystemLoader->addPath($templates); $formTemplate = $config["formTheme"] ?: "form_div_layout.html.twig"; $formTemplate = (array) $formTemplate; $formEngine = new \Symfony\Bridge\Twig\Form\TwigRendererEngine($formTemplate); $formEngine->setEnvironment($render); $extension = new \Symfony\Bridge\Twig\Extension\FormExtension(new \Symfony\Bridge\Twig\Form\TwigRenderer($formEngine, $this->getFormCsrfProvider())); break; default: $extension = new $extension(); } return $extension; }
public function render($echo = false) { // Load template directories. $loader = new \Twig_Loader_Filesystem(); $loader->addPath('templates'); // Set up Twig. $twig = new \Twig_Environment($loader, array('debug' => true, 'strct_variables' => true)); $twig->addExtension(new \Twig_Extension_Debug()); // Mardown support. $twig->addFilter(new \Twig_SimpleFilter('markdown', function ($text) { $parsedown = new \Parsedown(); return $parsedown->text($text); })); // DB queries. $twig->addFunction(new \Twig_SimpleFunction('db_queries', function () { return Db::getQueries(); })); // Render. $string = $twig->render($this->template, $this->data); if ($echo) { echo $string; } else { return $string; } }
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()); } }
/** * {@inheritdoc} */ public function register(Container $app) { $app['twig.path'] = array($app['app.templates.path']); $app['twig.templates'] = array(); $app['twig.loader'] = function () use($app) { $loaders = []; $twigLoaderFs = new \Twig_Loader_Filesystem($app['twig.path']); foreach ($app['extensions'] as $info) { if (!is_dir($templateViewDirectory = $info['pathName'] . '/' . self::EXTENSION_TEMPLATE_PATH)) { throw new InvalidTemplateDirectoryException(sprintf('"%s" is not a directory', $templateViewDirectory)); } $currentController = $app['request']->get('_controller'); if (strstr($currentController, '\\', true) === $info['name']) { $twigLoaderFs->addPath($templateViewDirectory); break; } } $loaders[] = $twigLoaderFs; $loaders[] = new \Twig_Loader_Array($app['twig.templates']); return new \Twig_Loader_Chain($loaders); }; $app['twig.environment'] = function () use($app) { $isTemplateMustBeCached = $app['twig.cache_templates']; $templateCacheDirectory = $app['twig.cache.directory']; $options = []; if ($isTemplateMustBeCached && $this->isTemplateCacheDirectoryValid($templateCacheDirectory)) { $options = ['cache' => $templateCacheDirectory]; } return new \Twig_Environment($app['twig.loader'], $options); }; $app['twig'] = function () use($app) { return $app['twig.environment']; }; }
public function dump() { $finder = new Finder(); $twigNamespaces = $this->loader->getNamespaces(); foreach ($twigNamespaces as $ns) { if (count($this->loader->getPaths($ns)) > 0) { $iterator = $finder->files()->in($this->loader->getPaths($ns)); foreach ($iterator as $file) { /** @var SplFileInfo $file */ $resource = new TwigResource($this->loader, '@' . $ns . '/' . $file->getRelativePathname()); $this->lam->addResource($resource, 'twig'); } } } foreach ($this->lam->getNames() as $name) { $asset = $this->lam->get($name); $formula = $this->lam->getFormula($name); $debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->lam->isDebug(); $combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : null; if (null !== $combine ? !$combine : $debug) { foreach ($asset as $leaf) { $this->aw->writeAsset($leaf); } } else { $this->aw->writeAsset($asset); } } }
/** * @throws \Twig_Error_Loader */ protected function setUp() { // Setup factory for tabs $this->tabFactory = Forms::createFormFactory(); parent::setUp(); $rendererEngine = new TwigRendererEngine(array('form_div_layout.html.twig', 'fields.html.twig')); if (interface_exists('Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface')) { $csrfProviderInterface = 'Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface'; } else { $csrfProviderInterface = 'Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface'; } $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfProviderInterface)); $this->extension = new FormExtension($renderer); $reflection = new \ReflectionClass($renderer); $bridgeDirectory = dirname($reflection->getFileName()) . '/../Resources/views/Form'; $loader = new \Twig_Loader_Filesystem(array($bridgeDirectory, __DIR__ . '/../../Resources/views/Form')); $loader->addPath(__DIR__ . '/../../Resources/views', 'MopaBootstrap'); $environment = new Twig_Environment($loader, array('strict_variables' => true)); $environment->addExtension(new TranslationExtension(new StubTranslator())); $environment->addExtension(new IconExtension('fontawesome')); $environment->addExtension(new FormExtension2()); $environment->addGlobal('global', ''); $environment->addExtension($this->extension); $this->extension->initRuntime($environment); }
/** * {@inheritDoc} */ public function createConfig(array $config = array()) { $config = ArrayObject::ensureArrayObject($config); $config->defaults($this->defaultConfig); $config->defaults(array('payum.template.layout' => '@PayumCore/layout.html.twig', 'payum.http_client' => HttpClientFactory::create(), 'guzzle.client' => HttpClientFactory::createGuzzle(), 'twig.env' => function (ArrayObject $config) { $loader = new \Twig_Loader_Filesystem(); foreach ($config['payum.paths'] as $namespace => $path) { $loader->addPath($path, $namespace); } return new \Twig_Environment($loader); }, 'payum.action.get_http_request' => new GetHttpRequestAction(), 'payum.action.capture_payment' => new CapturePaymentAction(), 'payum.action.execute_same_request_with_model_details' => new ExecuteSameRequestWithModelDetailsAction(), 'payum.action.render_template' => function (ArrayObject $config) { return new RenderTemplateAction($config['twig.env'], $config['payum.template.layout']); }, 'payum.extension.endless_cycle_detector' => new EndlessCycleDetectorExtension(), 'payum.action.get_currency' => function (ArrayObject $config) { return new GetCurrencyAction($config['payum.iso4217']); }, 'payum.prepend_actions' => array(), 'payum.prepend_extensions' => array(), 'payum.prepend_apis' => array(), 'payum.default_options' => array(), 'payum.required_options' => array(), 'payum.api.http_client' => function (ArrayObject $config) { return $config['payum.http_client']; }, 'payum.security.token_storage' => null)); if ($config['payum.security.token_storage']) { $config['payum.action.get_token'] = function (ArrayObject $config) { return new GetTokenAction($config['payum.security.token_storage']); }; } $config['payum.paths'] = array_replace(['PayumCore' => __DIR__ . '/Resources/views'], $config['payum.paths'] ?: []); return (array) $config; }
protected function getTwig() { $options = ['cache' => false, 'strict_variables' => true]; $loader = new \Twig_Loader_Filesystem(); $loader->addPath($this->skeletonDirs); return new \Twig_Environment($loader, $options); }
public function testGetNamespaces() { $loader = new Twig_Loader_Filesystem(sys_get_temp_dir()); $this->assertEquals(array('__main__'), $loader->getNamespaces()); $loader->addPath(sys_get_temp_dir(), 'named'); $this->assertEquals(array('__main__', 'named'), $loader->getNamespaces()); }
public function initRuntime(\Twig_Environment $environment) { $this->twigEnvironment = $environment; $loader = new \Twig_Loader_Filesystem(); $loader->addPath(__DIR__ . '/../Resorces/views', 'np_navigation'); $environment->getLoader()->addLoader($loader); }
public static function getInstance() { if (self::$instance !== null) { return self::$instance; } $loader = new \Twig_Loader_Filesystem(); $translator = Translator::getInstance(); $modules = \SimpleSAML_Module::getModules(); foreach ($modules as $module) { if (\SimpleSAML_Module::isModuleEnabled($module)) { $path = \SimpleSAML_Module::getModuleDir($module); $templatePath = self::resourceExists('templates', $path); if (false !== $templatePath) { $loader->addPath($templatePath, $module); } $translationPath = self::resourceExists('translations', $path); if (false !== $translationPath) { $translations = new Finder(); $translations->files()->in($translationPath)->name('/\\.[a-zA-Z_]+\\.yml$/'); /** @var SplFileInfo $translation */ foreach ($translations as $translation) { $name = $translation->getBasename('.yml'); $locale = substr($name, strrpos($name, '.') + 1); $translator->addResource('yaml', $translation->getPathname(), $locale, $module); } } } } self::$instance = new \Twig_Environment($loader); self::$instance->addExtension(new TranslationExtension($translator)); return self::$instance; }
/** * Response constructor. * @param $view * @param array $param */ public function __construct($view, $param = []) { $loader = new \Twig_Loader_Filesystem(SRC_ROUTE . "/Views"); $loader->addPath(SRC_ROUTE . "/", ""); $this->_twig = new \Twig_Environment($loader, ["charset" => "utf-8", "debug" => true]); echo $this->_twig->render($view, $param); }
public function init() { $view_dir = [__DIR__ . '/../Views', __DIR__ . '/../Templates']; $twigConfig = []; if ($this->config['twig']['cache']) { $twigConfig["cache"] = $this->app['cache']['twig']; } $twigConfig["debug"] = $this->config['twig']['debug']; $loader = new \Twig_Loader_Filesystem($view_dir); foreach ($view_dir as $d) { $loader->addPath($d, 'Meister'); } foreach ($this->config['modules'] as $app) { if (file_exists($this->app['Modules'] . $app . '/Views')) { $loader->addPath($this->app['Modules'] . $app . '/Views', $app); } if (file_exists($this->app['Modules'] . $app . '/Templates')) { $loader->addPath($this->app['Modules'] . $app . '/Templates', $app); } } $this->twig = new \Twig_Environment($loader, $twigConfig); $this->twig->addExtension(new \Twig_Extensions_Extension_I18n()); /** * Verifica permissões para exibir determinada coisa */ $function = new \Twig_SimpleFunction('permission', function ($rule) { return $this->app['auth']->checkRules($rule); }); $this->twig->addFunction($function); }
/** * TwigFilesystemProvider constructor. * @param \Twig_Loader_Filesystem $loader * @param string $root_path * @param string $namespace */ public function __construct(\Twig_Loader_Filesystem $loader, $root_path, $namespace) { $this->loader = $loader; $this->namespace = $namespace; if (!in_array($namespace, $loader->getNamespaces())) { $loader->addPath($root_path, $namespace); } }
public static function paths() { $loader = new Twig_Loader_Filesystem(TEMPLATES); $loader->addPath(TEMPLATES . "module/user/", 'ModuleUser'); $loader->addPath(TEMPLATES . "module/login/", 'ModuleLogin'); $loader->addPath(TEMPLATES . "modales/", 'Modal'); return $loader; }
private function createLoader($templatePath) { $loader = new \Twig_Loader_Filesystem($templatePath); $loader->addPath($templatePath . "/", "root"); $loader->addPath($templatePath . "/components", "components"); $loader->addPath($templatePath . "/template", "template"); return $loader; }
/** * @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'); }
/** @Provides("Twig_LoaderInterface") */ static function provideLoader(array $namespaces = [], array $paths = []) { $loader = new \Twig_Loader_Filesystem("/"); foreach (array_combine($namespaces, $paths) as $ns => $path) { $loader->addPath($path, $ns); } return $loader; }
/** * @return \Twig_Environment */ public static function createGeneric() { $loader = new \Twig_Loader_Filesystem(); foreach (static::createGenericPaths() as $path => $namespace) { $loader->addPath($path, $namespace); } return new \Twig_Environment($loader); }
private function createLoader($templatePath) { $loader = new \Twig_Loader_Filesystem($templatePath); $loader->addPath($_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/", "templateRoot"); $loader->addPath($_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/template/", "template"); $loader->addPath($_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/components/", "templateComponents"); return $loader; }
/** * @return \Twig_Environment */ public static function createGeneric() { $paths = array_filter(array('PayumCore' => self::guessViewsPath('Payum\\Core\\Payment'), 'PayumStripe' => self::guessViewsPath('Payum\\Stripe\\PaymentFactory'), 'PayumKlarnaCheckout' => self::guessViewsPath('Payum\\Klarna\\Checkout\\PaymentFactory'))); $loader = new \Twig_Loader_Filesystem(); foreach ($paths as $namespace => $path) { $loader->addPath($path, $namespace); } return new \Twig_Environment($loader); }
/** * @dataProvider getSecurityTests */ public function testSecurity($template) { $loader = new Twig_Loader_Filesystem(array(dirname(__FILE__) . '/../Fixtures')); try { $loader->getCacheKey($template); $this->fail(); } catch (Twig_Error_Loader $e) { $this->assertNotContains('Unable to find template', $e->getMessage()); } }
/** * 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; }
/** * @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); }
/** * @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; }); }
/** * {@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 \Twig_Error_Loader(sprintf("Template \"%s\" not found. Tried the following:\n%s", $name, implode("\n", $templates))); }
public static function getInstance() { if (!self::$_instance) { $dir = __DIR__ . '/'; $loader = new \Twig_Loader_Filesystem($dir . '../../../app/views/'); $loader->addPath($dir . '../../../app/views/admin/', 'admin'); self::$_instance = new \Twig_Environment($loader, array('xxxcache' => $dir . '/cache/')); require __DIR__ . '/TwigExtension.php'; self::$_instance->addExtension(new TwigExtension()); } return self::$_instance; }