private function getTemplate($template, $container) { $loader = new \Twig_Loader_Array(array('index' => $template)); $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false)); $twig->addExtension(new DataGridExtension($container)); return $twig->loadTemplate('index'); }
public function renderBreadcrumbs(\Twig_Environment $twig, Breadcrumbs $breadcrumbs = null, $template = '@Breadcrumbs/breadcrumbs.html.twig') { if (null === $breadcrumbs) { $breadcrumbs = $this->breadcrumbsBuilder->createFromRequest(); } return $twig->render($template, array('breadcrumbs' => $breadcrumbs)); }
public function loadTemplate($file, $variables) { $loader = new \Twig_Loader_Filesystem(\AHContentBlockerServer\TEMPLATES_PATH_MAIN); $twig = new \Twig_Environment($loader, array('cache' => \AHContentBlockerServer\TEMPLATES_PATH_CACHE)); $loginTemplate = $twig->loadTemplate($file); return $loginTemplate->render($variables); }
/** * Constructor * * @param string $tmplPath * @param array $extraParams * @return void */ public function __construct($tmplPath = null, $extraParams = array()) { Twig_Autoloader::register(); $loader = new Twig_Loader_Filesystem($tmplPath); $cache_path = (array_key_exists('cache_path', $extraParams)) ? $extraParams['cache_path'] : '../cache' ; $view = new Twig_Environment($loader, array( 'cache' => $cache_path, 'auto_reload' => true, ) ); $escaper = new Twig_Extension_Escaper(true); $view->addExtension($escaper); $this->_twig = $view; if (null !== $tmplPath) { $this->setScriptPath($tmplPath); } foreach ($extraParams as $key => $value) { $this->_twig->{$key} = $value; } }
public function getTests() { $environment = new Twig_Environment(); $environment->addFunction('foo', new Twig_Function_Function('foo', array())); $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true))); $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true))); $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true))); $tests = array(); $node = $this->createFunction('foo'); $tests[] = array($node, 'foo()', $environment); $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1))); $tests[] = array($node, 'foo("bar", "foobar")', $environment); $node = $this->createFunction('bar'); $tests[] = array($node, 'bar($this->env)', $environment); $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 1))); $tests[] = array($node, 'bar($this->env, "bar")', $environment); $node = $this->createFunction('foofoo'); $tests[] = array($node, 'foofoo($context)', $environment); $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 1))); $tests[] = array($node, 'foofoo($context, "bar")', $environment); $node = $this->createFunction('foobar'); $tests[] = array($node, 'foobar($this->env, $context)', $environment); $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 1))); $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment); // named arguments $node = $this->createFunction('date', array('timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1), 'date' => new Twig_Node_Expression_Constant(0, 1))); $tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")'); return $tests; }
protected function setUp() { if (!class_exists('Symfony\\Component\\Locale\\Locale')) { $this->markTestSkipped('The "Locale" component is not available'); } if (!class_exists('Symfony\\Component\\EventDispatcher\\EventDispatcher')) { $this->markTestSkipped('The "EventDispatcher" component is not available'); } if (!class_exists('Symfony\\Component\\Form\\Form')) { $this->markTestSkipped('The "Form" component is not available'); } if (!class_exists('Twig_Environment')) { $this->markTestSkipped('Twig is not available.'); } parent::setUp(); $rendererEngine = new TwigRendererEngine(array('form_table_layout.html.twig', 'custom_widgets.html.twig')); $renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface')); $this->extension = new FormExtension($renderer); $loader = new StubFilesystemLoader(array(__DIR__ . '/../../Resources/views/Form', __DIR__)); $environment = new \Twig_Environment($loader, array('strict_variables' => true)); $environment->addExtension(new TranslationExtension(new StubTranslator())); $environment->addGlobal('global', ''); $environment->addExtension($this->extension); $this->extension->initRuntime($environment); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); if (!in_array($this->type, array('form', 'filter'))) { throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)'); } $rendererEngine = new TwigRendererEngine(array($this->type . '_admin_fields.html.twig')); $csrfManagerClass = interface_exists('Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface') ? 'Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface' : 'Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface'; $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass)); $this->extension = new FormExtension($renderer); $twigPaths = array(__DIR__ . '/../../../Resources/views/Form'); //this is ugly workaround for different build strategies and, possibly, //different TwigBridge installation directories if (is_dir(__DIR__ . '/../../../vendor/symfony/twig-bridge/Resources/views/Form')) { $twigPaths[] = __DIR__ . '/../../../vendor/symfony/twig-bridge/Resources/views/Form'; } elseif (is_dir(__DIR__ . '/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form')) { $twigPaths[] = __DIR__ . '/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form'; } else { $twigPaths[] = __DIR__ . '/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form'; } $loader = new StubFilesystemLoader($twigPaths); $this->environment = new \Twig_Environment($loader, array('strict_variables' => true)); $this->environment->addGlobal('sonata_admin', $this->getSonataAdmin()); $this->environment->addExtension(new TranslationExtension(new StubTranslator())); $this->environment->addExtension($this->extension); $this->extension->initRuntime($this->environment); }
/** * Tests the escaping * * @dataProvider providerTestEscaping */ public function testEscaping($template, $expected) { $twig = new \Twig_Environment(NULL, array('debug' => TRUE, 'cache' => FALSE, 'autoescape' => TRUE, 'optimizations' => 0)); $twig->addExtension((new TwigExtension())->setGenerators($this->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface'))); $nodes = $twig->parse($twig->tokenize($template)); $this->assertSame($expected, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter); }
public static function view($modules = NULL) { include_once __ROOT__ . 'vendor/autoload.php'; $views = __VIEWS__; if (__ENV_MODE__ == "dev") { $cache = false; } else { $cache = true; } $loader = new \Twig_Loader_Filesystem($views); // Dossier contenant les templates if ($cache) { $twig = new \Twig_Environment($loader, array('debug' => false, 'cache' => $views . '/cache/', 'auto_reload' => true)); } else { $twig = new \Twig_Environment($loader, array('debug' => true)); } $twig->addExtension(new \Twig_Extensions_Extension_Text()); $twig->addExtension(new \Twig_Extensions_Extension_I18n()); $twig->addExtension(new \Twig_Extensions_Extension_Intl()); $twig->addExtension(new \Twig_Extensions_Extension_Array()); $twig->addExtension(new \Twig_Extensions_Extension_Date()); $twig->addGlobal('app_name', __NAME_APPLICATION__); $path = array('views' => $views, 'url' => __URL_LOCAL__, 'bootstrap' => __BOOTSTRAP__); $twig->addGlobal('path', $path); $route = array('only' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); $twig->addGlobal('route', $route); return $twig; }
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 function __construct(\Twig_Environment $twig = null, array $config = array()) { if ($twig) { $this->twig = $twig; $this->twig->addGlobal('ctrl_rad_templates', $config['templates']); } }
public function indexAction() { // $this->init($app); $posts = $this->db->fetchAll('SELECT * FROM post'); //var_dump($posts); return $this->twig->render('post/index.twig', array('posts' => $posts)); }
/** * @return \Twig_Environment */ protected function createEnvironment() { $loader = new \Twig_Loader_Array([]); $twig = new \Twig_Environment($loader, ['debug' => true, 'cache' => false, 'autoescape' => false]); $twig->addExtension($this->createExtension()); return $twig; }
public function setUp() { $this->cmfHelper = $this->getMockBuilder('Symfony\\Cmf\\Bundle\\CoreBundle\\Templating\\Helper\\CmfHelper')->disableOriginalConstructor()->getMock(); $this->cmfExtension = new CmfExtension($this->cmfHelper); $this->env = new \Twig_Environment(new \Twig_Loader_Array(array())); $this->env->addExtension($this->cmfExtension); }
/** * @return \Twig_Template */ protected function getIconTemplate(\Twig_Environment $env) { if ($this->iconTemplate === null) { $this->iconTemplate = $env->loadTemplate('@MopaBootstrap/icons.html.twig'); } return $this->iconTemplate; }
/** * Adds the WP-specific filters and functions to the twig environment * @param \Twig_Environment $twig_Environment */ private static function extendTwig(\Twig_Environment $twig_Environment) { $twig_Environment->addFilter(new \Twig_SimpleFilter('__', function ($text, $domain = 'default') { return __($text, $domain); })); $twig_Environment->addExtension(new SlugifyExtension(Slugify::create())); }
private function template() { $config = new Config($this->registry); $twigPath = $config->getTWIGPath(); $this->themeUrl = $this->registry->siteUrl . 'themes/' . $this->registry->template . '/'; $this->isAjaxRequest = $this->registry->dispatcher->isAjaxRequest(); require $twigPath; \Twig_Autoloader::register(); $loader = new \Twig_Loader_Filesystem($this->getTemplate()); $twig = new \Twig_Environment($loader); $urlFunction = new \Twig_SimpleFunction('url', function ($ctrl_action, $paramsArray = NULL) { $ctrl_action = explode('/', $ctrl_action); $controller = $ctrl_action[0] . '/'; $action = $ctrl_action[1] . '/'; $params = ''; if (isset($paramsArray)) { $params .= '?'; foreach ($paramsArray as $key => $value) { $params .= $key . '=' . $value; if (end($paramsArray) != $value) { $params .= '&'; } } } $url = $this->registry->siteUrl . $controller . $action . $params; return $url; }); $twig->addFunction($urlFunction); $template = $twig->loadTemplate($this->getView()); return $template; }
public function render($templateName) { $loader = new \Twig_Loader_Filesystem("../source/app/templates/"); $twig = new \Twig_Environment($loader, array()); $template = $twig->loadTemplate("{$templateName}.html"); return $template->render(array()); }
public function register(Application $app) { if (!isset($app['twig'])) { $app->register(new \Silex\Provider\TwigServiceProvider()); } // Handlers $app['twig.handlers'] = $app->share(function (Application $app) { return new \Pimple(['admin' => $app->share(function () use($app) { return new AdminHandler($app); }), 'array' => $app->share(function () use($app) { return new ArrayHandler($app); }), 'html' => $app->share(function () use($app) { return new HtmlHandler($app); }), 'image' => $app->share(function () use($app) { return new ImageHandler($app); }), 'record' => $app->share(function () use($app) { return new RecordHandler($app); }), 'text' => $app->share(function () use($app) { return new TextHandler($app); }), 'user' => $app->share(function () use($app) { return new UserHandler($app); }), 'utils' => $app->share(function () use($app) { return new UtilsHandler($app); })]); }); // Add the Bolt Twig Extension. $app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig, $app) { $twig->addExtension(new TwigExtension($app, $app['twig.handlers'], false)); return $twig; })); $app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', function ($filesystem, $app) { $filesystem->addPath($app['resources']->getPath('app/view/twig'), 'bolt'); // @deprecated Since Bolt 2.3 and will be removed in Bolt 3. $filesystem->addPath($app['resources']->getPath('app/view/twig')); return $filesystem; })); // Twig paths $app['twig.path'] = function () use($app) { return $app['config']->getTwigPath(); }; // Twig options $app['twig.options'] = function () use($app) { // Should we cache or not? if ($app['config']->get('general/caching/templates')) { $cache = $app['resources']->getPath('cache'); } else { $cache = false; } return ['debug' => true, 'cache' => $cache, 'strict_variables' => $app['config']->get('general/strict_variables'), 'autoescape' => true]; }; $app['safe_twig.bolt_extension'] = function () use($app) { return new TwigExtension($app, $app['twig.handlers'], true); }; $app['safe_twig'] = $app->share(function ($app) { $loader = new \Twig_Loader_String(); $twig = new \Twig_Environment($loader); $twig->addExtension($app['safe_twig.bolt_extension']); return $twig; }); }
public function render() { $loader = new Twig_Loader_Filesystem(__DIR__ . '/../template/'); $twig = new Twig_Environment($loader, array('debug' => true)); $template = $twig->loadTemplate($this->layout); return $template->render($this->arrayVar); }
public function render() { delete_transient($this->transient_notices); $loader = new \Twig_Loader_Filesystem($this->paths); $twig = new \Twig_Environment($loader); // Add the admin_url() function. $twig->addFunction('admin_url', new \Twig_SimpleFunction('admin_url', 'admin_url')); // Add titlecase filter. $titlecase_filter = new \Twig_SimpleFilter('titlecase', '\\WordPress\\Tabulate\\Text::titlecase'); $twig->addFilter($titlecase_filter); // Add date and time filters. $date_filter = new \Twig_SimpleFilter('wp_date_format', '\\WordPress\\Tabulate\\Text::wp_date_format'); $twig->addFilter($date_filter); $time_filter = new \Twig_SimpleFilter('wp_time_format', '\\WordPress\\Tabulate\\Text::wp_time_format'); $twig->addFilter($time_filter); // Add strtolower filter. $strtolower_filter = new \Twig_SimpleFilter('strtolower', function ($str) { if (is_array($str)) { return array_map('strtolower', $str); } else { return strtolower($str); } }); $twig->addFilter($strtolower_filter); // Enable debugging. if (WP_DEBUG) { $twig->enableDebug(); $twig->addExtension(new \Twig_Extension_Debug()); } // Render the template. $template = $twig->loadTemplate($this->templateName); return $template->render($this->data); }
/** * Function returning the Twig environnment * * @return \Twig_Environment */ private function getTwigEnvironment() { $arrcache = array(); if (TEMPLATE_CACHE) { $cachepath = CACHEPATH . DS . 'twig'; $arrcache['cache'] = $cachepath; $arrcache['auto_reload'] = AUTORELOADCACHE; } if (DEVELOPMENT_ENVIRONMENT) { $arrcache['debug'] = true; } $twigloader = new \Twig_Loader_Filesystem($this->getPathArray()); $twigenv = new \Twig_Environment($twigloader, $arrcache); $twigenv->addExtension(new \GL\Core\Twig\TwigHelper($this->_container)); // add shared TwigHelper $config = new \GL\Core\Config\Config('twig'); $value = $config->load(); foreach ($value as $name => $th) { $class = $th['class']; $twigenv->addExtension(new $class($this->_container)); } //$twigenv->addExtension(new \Application\Shared\SharedTwigHelper($this->_container)); if (DEVELOPMENT_ENVIRONMENT) { $twigenv->addExtension(new \GL\Core\Debug\TwigDebugBar($this->_container)); $twigenv->addExtension(new \Twig_Extension_Debug()); $this->_profile = new \Twig_Profiler_Profile(); $twigenv->addExtension(new \Twig_Extension_Profiler($this->_profile)); } $twigenv->addTokenParser(new \GL\Core\Twig\TwigRenderToken()); $twigenv->addTokenParser(new \GL\Core\Twig\TwigRouteToken()); return $twigenv; }
/** * Return the rendered content of a template. * @param string $template * @param array $variables * @return string */ public function getTemplateContent($template, array $variables) { $dir = dirname($template); $file = basename($template); $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array($dir)), array('debug' => true, 'cache' => false, 'strict_variables' => true, 'autoescape' => false)); return $twig->render($file, $variables); }
protected function getEnvironment() { $env = new Twig_Environment(); $env->addFunction(new Twig_Function('anonymous', function () { })); return $env; }
/** * @dataProvider getEscapingTemplates */ public function testEscaping($template, $mustBeEscaped) { $twig = new \Twig_Environment(null, array('debug' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0)); $twig->addExtension(new RoutingExtension($this->getMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface'))); $nodes = $twig->parse($twig->tokenize($template)); $this->assertSame($mustBeEscaped, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter); }
/** * @param string $path * * @return array|Word[] */ public function parseFile($path) { $words = array(); $twig = new \Twig_Environment(); // TODO should it be configured dynamically? $lines = file($path); if (count($lines) <= 0) { // TODO handle and log error return array(); } $tokenStream = $twig->tokenize(implode('', $lines)); while (!$tokenStream->isEOF()) { $token = $tokenStream->next(); if ($token->getType() === \Twig_Token::TEXT_TYPE) { $lineNumber = $token->getLine(); //split line by line foreach (preg_split("/((\r?\n)|(\r\n?))/", $token->getValue()) as $line) { $parts = array(); preg_match_all('/(([[:alpha:]]+[\'-])*[[:alpha:]]+\'?)/u', strip_tags($line), $parts, PREG_SET_ORDER); foreach ($parts as $part) { $words[] = new Word(trim($part[0]), $path, $lines[$lineNumber - 1], $lineNumber); } ++$lineNumber; } } } return $words; }
public function dump(\Twig_Environment $env, $context) { if (!$env->isDebug()) { return; } if (2 === func_num_args()) { $vars = array(); foreach ($context as $key => $value) { if (!$value instanceof \Twig_Template) { $vars[$key] = $value; } } $vars = array($vars); } else { $vars = func_get_args(); unset($vars[0], $vars[1]); } $dump = fopen('php://memory', 'r+b'); $dumper = new HtmlDumper($dump); foreach ($vars as $value) { $dumper->dump($this->cloner->cloneVar($value)); } rewind($dump); return stream_get_contents($dump); }
/** * * @param String $templateDir * @return \Twig_Environment */ public static function getTwig($templateDir) { $loader = new \Twig_Loader_Filesystem($templateDir); $twig = new \Twig_Environment($loader, array('debug' => true)); $twig->addExtension(new Yep\TracyTwigExtensions\BarDumpExtension()); return $twig; }
public function getTests() { $environment = new Twig_Environment(); $environment->addFunction('foo', new Twig_Function_Function('foo', array())); $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true))); $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true))); $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true))); $tests = array(); $node = $this->createFunction('foo'); $tests[] = array($node, 'foo()', $environment); $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 0), new Twig_Node_Expression_Constant('foobar', 0))); $tests[] = array($node, 'foo("bar", "foobar")', $environment); $node = $this->createFunction('bar'); $tests[] = array($node, 'bar($this->env)', $environment); $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 0))); $tests[] = array($node, 'bar($this->env, "bar")', $environment); $node = $this->createFunction('foofoo'); $tests[] = array($node, 'foofoo($context)', $environment); $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 0))); $tests[] = array($node, 'foofoo($context, "bar")', $environment); $node = $this->createFunction('foobar'); $tests[] = array($node, 'foobar($this->env, $context)', $environment); $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 0))); $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment); return $tests; }
/** * Constructor. * * @param \Twig_Environment $environment A \Twig_Environment instance * @param ContainerInterface $container The DI container * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance * @param GlobalVariables $globals A GlobalVariables instance */ public function __construct(\Twig_Environment $environment, ContainerInterface $container, TemplateNameParserInterface $parser, GlobalVariables $globals) { $this->environment = $environment; $this->container = $container; $this->parser = $parser; $environment->addGlobal('app', $globals); }