mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 09:26:29 +00:00
added a recipe for stateful node visitors
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user