added a recipe for stateful node visitors

This commit is contained in:
Fabien Potencier
2012-01-07 09:53:33 +01:00
parent 260505129e
commit fa08e76f09
+23
View File
@@ -275,4 +275,27 @@ rewrites the cache::
}
}
Reusing a stateful Node Visitor
-------------------------------
When attaching a visitor to a ``Twig_Environment`` instance, Twig uses it to
visit *all* templates it compiles. If you need to keep some state information
around, you probably want to reset it when visiting a new template.
This can be easily achieved with the following code::
protected $someTemplateState = array();
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
{
if ($node instanceof Twig_Node_Module) {
// reset the state as we are entering a new template
$this->someTemplateState = array();
}
// ...
return $node;
}
.. _callback: http://www.php.net/manual/en/function.is-callable.php