public function testCustomEscaper() { $twig = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface')); $twig->getExtension('core')->setEscaper('foo', 'foo_escaper_for_test'); $this->assertEquals('fooUTF-8', twig_escape_filter($twig, 'foo', 'foo')); $this->assertEquals('UTF-8', twig_escape_filter($twig, null, 'foo')); $this->assertEquals('42UTF-8', twig_escape_filter($twig, 42, 'foo')); }
/** * {@inheritdoc} */ protected function doEnterNode(Apishka_Templater_NodeAbstract $node, Apishka_Templater_Environment $env) { if ($node instanceof Apishka_Templater_Node_Module) { if ($env->hasExtension('escaper') && ($defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename')))) { $this->defaultStrategy = $defaultStrategy; } $this->safeVars = array(); } elseif ($node instanceof Apishka_Templater_Node_AutoEscape) { $this->statusStack[] = $node->getAttribute('value'); } elseif ($node instanceof Apishka_Templater_Node_Block) { $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env); } return $node; }
public function testAddMockExtension() { $extension = $this->createMock('Apishka_Templater_ExtensionInterface'); $extension->expects($this->once())->method('getName')->will($this->returnValue('mock')); $loader = new Apishka_Templater_Loader_Array(array('page' => 'hey')); $twig = new Apishka_Templater_Environment($loader); $twig->addExtension($extension); $this->assertInstanceOf('Apishka_Templater_ExtensionInterface', $twig->getExtension('mock')); $this->assertTrue($twig->isTemplateFresh('page', time())); }
/** * Renders a template. * * @param Apishka_Templater_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(Apishka_Templater_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(); } } $result = null; try { $result = $env->resolveTemplate($template)->render($variables); } catch (Apishka_Templater_Error_Loader $e) { if (!$ignoreMissing) { if ($isSandboxed && !$alreadySandboxed) { $sandbox->disableSandbox(); } throw $e; } } if ($isSandboxed && !$alreadySandboxed) { $sandbox->disableSandbox(); } return $result; }