/** * {@inheritDoc} */ public function process($layoutSrc, Response $response, array $placeResponses) { if (!$this->twig->hasExtension('supraPage')) { throw new \UnexpectedValueException('Missing for Supra Page extension.'); } $response->setContent($this->twig->render($layoutSrc, array('responses' => $placeResponses))); }
/** * Echoes the <script> tag. * */ public function includeAceEditor() { if (!$this->environment->hasExtension('asset') || $this->editorIncluded) { return; } if (!$this->editorIncluded) { foreach (array('ace', 'ext-language_tools') as $file) { $jsPath = $this->environment->getExtension('asset')->getAssetUrl($this->basePath . '/' . $this->mode . '/' . $file . '.js'); echo sprintf('<script src="%s" charset="utf-8" type="text/javascript"></script>', $jsPath); } $this->editorIncluded = true; } }
public function includeAceEditor() { if (!$this->environment->hasExtension('assets')) { return; } if (!$this->editorIncluded) { $this->editorIncluded = true; } if (!$this->ckeditorIncluded) { $jsPath = $this->environment->getExtension('assets')->getAssetUrl($this->basePath . '/' . $this->mode); echo sprintf('<script src="%s" charset="utf-8"></script>', $jsPath); $this->ckeditorIncluded = true; } }
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) { if ($node instanceof \Twig_Node_Block) { $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needCollapsing(); } elseif ($node instanceof WhitespaceCollapse) { $this->statusStack[] = $node->getAttribute('value'); } elseif ($env->hasExtension('whitespace_collapser')) { /** @var \MatTheCat\Twig\Extension\WhitespaceCollapser $extension */ $extension = $env->getExtension('whitespace_collapser'); $extensionDefault = $extension->getDefault(); if ($node instanceof \Twig_Node_Module) { if (is_array($extensionDefault)) { $filename = $node->getAttribute('filename'); if (substr($filename, -5) === '.twig') { $filename = substr($filename, 0, -5); } $this->enabledByDefault = in_array(pathinfo($filename, PATHINFO_EXTENSION), $extensionDefault, true); } else { $this->enabledByDefault = $extensionDefault; } } elseif ($node instanceof \Twig_Node_AutoEscape) { $this->statusStack[] = is_array($extensionDefault) ? in_array($node->getAttribute('value'), $extensionDefault) : $extensionDefault; } } return $node; }
protected function needEscaping(Twig_Environment $env) { if (count($this->statusStack)) { return $this->statusStack[count($this->statusStack) - 1]; } else { return $env->hasExtension('escaper') ? $env->getExtension('escaper')->isGlobal() : false; } }
public function __construct(\Twig_Environment $environment) { if (!$environment->hasExtension('form')) { throw new \DomainException('The FormRenderer needs an environment with the FormExtension'); } $this->environment = $environment; $this->environment->getExtension('form')->initRuntime($this->environment); }
function let(\Twig_Environment $environment, AssetsExtension $assets, FSiFilePathResolver $filePathResolver) { $this->beConstructedWith($filePathResolver); $environment->hasExtension('assets')->shouldBeCalled()->willReturn(true); $environment->getExtension('assets')->shouldBeCalled()->willReturn($assets); $environment->getGlobals()->shouldBeCalled()->willReturn(array()); $this->initRuntime($environment); }
/** * Optimizes print nodes. * * It replaces: * * * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()" * * "echo $this->getContext('...')" with "if (isset('...')) { echo '...' }" * * @param Twig_NodeInterface $node A Node * @param Twig_Environment $env The current Twig environment */ protected function optimizePrintNode($node, $env) { if (!$node instanceof Twig_Node_Print) { return $node; } if ($node->getNode('expr') instanceof Twig_Node_Expression_BlockReference || $node->getNode('expr') instanceof Twig_Node_Expression_Parent || $node->getNode('expr') instanceof Twig_Node_Expression_Name && !$env->hasExtension('sandbox') && !$env->isStrictVariables()) { $node->getNode('expr')->setAttribute('output', true); return $node->getNode('expr'); } return $node; }
/** * Called before child nodes are visited. * * @param Twig_NodeInterface $node The node to visit * @param Twig_Environment $env The Twig environment instance * * @return Twig_NodeInterface The modified node */ public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { if ($env->hasExtension('escaper') && ($defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename')))) { $this->defaultStrategy = $defaultStrategy; } } elseif ($node instanceof Twig_Node_AutoEscape) { $this->statusStack[] = $node->getAttribute('value'); } elseif ($node instanceof Twig_Node_Block) { $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env); } return $node; }
/** * {@inheritdoc} */ protected function doEnterNode(Twig_Node $node, Twig_Environment $env) { if ($node instanceof Twig_Node_Module) { if ($env->hasExtension('escaper') && ($defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename')))) { $this->defaultStrategy = $defaultStrategy; } $this->safeVars = array(); } elseif ($node instanceof Twig_Node_AutoEscape) { $this->statusStack[] = $node->getAttribute('value'); } elseif ($node instanceof Twig_Node_Block) { $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env); } elseif ($node instanceof Twig_Node_Import) { $this->safeVars[] = $node->getNode('var')->getAttribute('name'); } return $node; }
/** * Renders a template. * * @param Twig_Environment $env * @param array $context * @param string|array $template The template to render or an array of templates to try consecutively * @param array $variables The variables to pass to the template * @param bool $withContext * @param bool $ignoreMissing Whether to ignore missing templates or not * @param bool $sandboxed Whether to sandbox the template or not * * @return string The rendered template */ function twig_include(Twig_Environment $env, $context, $template, $variables = array(), $withContext = true, $ignoreMissing = false, $sandboxed = false) { $alreadySandboxed = false; $sandbox = null; if ($withContext) { $variables = array_merge($context, $variables); } if ($isSandboxed = $sandboxed && $env->hasExtension('sandbox')) { $sandbox = $env->getExtension('sandbox'); if (!($alreadySandboxed = $sandbox->isSandboxed())) { $sandbox->enableSandbox(); } } try { return $env->resolveTemplate($template)->render($variables); } catch (Twig_Error_Loader $e) { if (!$ignoreMissing) { throw $e; } } if ($isSandboxed && !$alreadySandboxed) { $sandbox->disableSandbox(); } }
public function testHasGetExtensionByClassName() { $twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()); $twig->addExtension($ext = new Twig_Tests_EnvironmentTest_Extension()); $this->assertTrue($twig->hasExtension('Twig_Tests_EnvironmentTest_Extension')); $this->assertTrue($twig->hasExtension('\\Twig_Tests_EnvironmentTest_Extension')); $this->assertSame($ext, $twig->getExtension('Twig_Tests_EnvironmentTest_Extension')); $this->assertSame($ext, $twig->getExtension('\\Twig_Tests_EnvironmentTest_Extension')); }
/** * Inject extensions into the TwigEnvironment instance. * * @param TwigEnvironment $environment * @param ContainerInterface $container * @param array $extensions * @throws Exception\InvalidExtensionException */ private function injectExtensions(TwigEnvironment $environment, ContainerInterface $container, array $extensions) { foreach ($extensions as $extension) { // Load the extension from the container if (is_string($extension) && $container->has($extension)) { $extension = $container->get($extension); } if (!$extension instanceof Twig_ExtensionInterface) { throw new Exception\InvalidExtensionException(sprintf('Twig extension must be an instance of Twig_ExtensionInterface; "%s" given,', is_object($extension) ? get_class($extension) : gettype($extension))); } if ($environment->hasExtension($extension->getName())) { continue; } $environment->addExtension($extension); } }
protected function loadTwigExtensions(\Twig_Environment $twig) { foreach ($this->twigExtensions as $twigExtensionName) { if (is_object($twigExtensionName)) { $twigExtension = $twigExtensionName; } else { $twigExtension = new $twigExtensionName(); } if (!$twig->hasExtension($twigExtension->getName())) { $twig->addExtension($twigExtension); } } }