protected function TwigNodeVisitor::doLeaveNode

protected TwigNodeVisitor::doLeaveNode(\Twig_Node $node, \Twig_Environment $env)

Called after child nodes are visited.

Parameters

Twig_Node $node The node to visit:

Twig_Environment $env The Twig environment instance:

Return value

Twig_Node|false The modified node or false if the node must be removed

Overrides Twig_BaseNodeVisitor::doLeaveNode

File

core/lib/Drupal/Core/Template/TwigNodeVisitor.php, line 26

Class

TwigNodeVisitor
Provides a Twig_NodeVisitor to change the generated parse-tree.

Namespace

Drupal\Core\Template

Code

protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) {
  // We use this to inject a call to render_var -> TwigExtension->renderVar()
  // before anything is printed.
  if ($node instanceof \Twig_Node_Print) {
    if (!empty($this->skipRenderVarFunction)) {
      // No need to add the callback, we have escape active already.
      unset($this->skipRenderVarFunction);
      return $node;
    }
    $class = get_class($node);
    $line = $node->getLine();
    return new $class(
    new \Twig_Node_Expression_Function('render_var', new \Twig_Node(array($node->getNode('expr'))), $line), 
    $line
    );
  }
  // Change the 'escape' filter to our own 'drupal_escape' filter.
  elseif ($node instanceof \Twig_Node_Expression_Filter) {
    $name = $node->getNode('filter')->getAttribute('value');
    if ('escape' == $name || 'e' == $name) {
      // Use our own escape filter that is SafeMarkup aware.
      $node->getNode('filter')->setAttribute('value', 'drupal_escape');

      // Store that we have a filter active already that knows how to deal with render arrays.
      $this->skipRenderVarFunction = TRUE;
    }
  }

  return $node;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Template!TwigNodeVisitor.php/function/TwigNodeVisitor::doLeaveNode/8.1.x