Registers a Node Visitor.
public addNodeVisitor ( Twig_NodeVisitorInterface $visitor ) | ||
$visitor | Twig_NodeVisitorInterface | A Twig_NodeVisitorInterface instance |
/** * @dataProvider getAttributeExceptions */ public function testGetAttributeExceptions($template, $message, $useExt) { $name = 'index_' . ($useExt ? 1 : 0); $templates = array($name => $template . $useExt); $env = new Twig_Environment(new Twig_Loader_Array($templates), array('strict_variables' => true)); if (!$useExt) { $env->addNodeVisitor(new CExtDisablingNodeVisitor()); } $template = $env->loadTemplate($name); $context = array('string' => 'foo', 'array' => array('foo' => 'foo'), 'array_access' => new Twig_TemplateArrayAccessObject()); try { $template->render($context); } catch (Twig_Error_Runtime $e) { $this->assertEquals(sprintf($message, $name), $e->getMessage()); } }
/** * @dataProvider getAttributeExceptions */ public function testGetAttributeExceptions($template, $message, $useExt) { $name = 'index_' . ($useExt ? 1 : 0); $templates = array($name => $template . $useExt); $env = new Twig_Environment(new Twig_Loader_Array($templates), array('strict_variables' => true)); if (!$useExt) { $env->addNodeVisitor(new CExtDisablingNodeVisitor()); } $template = $env->loadTemplate($name); $context = array('string' => 'foo', 'null' => null, 'empty_array' => array(), 'array' => array('foo' => 'foo'), 'array_access' => new Twig_TemplateArrayAccessObject(), 'magic_exception' => new Twig_TemplateMagicPropertyObjectWithException(), 'object' => new stdClass()); try { $template->render($context); $this->fail('Accessing an invalid attribute should throw an exception.'); } catch (Twig_Error_Runtime $e) { $this->assertSame(sprintf($message, $name), $e->getMessage()); } }
private function createEnv() { $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); $env->addNodeVisitor(new Paginator()); $env->addFunction(new \Twig_SimpleFunction('paginate', function () { })); $env->addFunction(new \Twig_SimpleFunction('render_documents', function () { })); $env->addGlobal('collection', range(1, 100)); return $env; }