From 1aa60bfdd588281fd24b3e449efd1b3d896aa2e1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 10 Jun 2010 10:51:31 +0200 Subject: [PATCH] added some helper method to build common nodes more easily (experimental) --- lib/Twig/SimpleTokenParser.php | 37 +++++++++++++++++++++++++++++++++++++ 1 files changed, 37 insertions(+), 0 deletions(-) diff --git a/lib/Twig/SimpleTokenParser.php b/lib/Twig/SimpleTokenParser.php index 42e78da..64d4c86 100644 --- a/lib/Twig/SimpleTokenParser.php +++ b/lib/Twig/SimpleTokenParser.php @@ -45,6 +45,43 @@ abstract class Twig_SimpleTokenParser extends Twig_TokenParser */ abstract protected function getNode(array $values, $line); + protected function getAttribute($node, $attribute, $arguments = array(), $line = -1) + { + return new \Twig_Node_Expression_GetAttr( + $node instanceof Twig_NodeInterface ? $node : new \Twig_Node_Expression_Name($node, $line), + $attribute instanceof Twig_NodeInterface ? $attribute : new \Twig_Node_Expression_Constant($attribute, $line), + $arguments instanceof Twig_NodeInterface ? $arguments : new \Twig_Node($arguments), + \Twig_Node_Expression_GetAttr::TYPE_ANY, + $line + ); + } + + protected function markAsSafe(Twig_NodeInterface $node, $line = -1) + { + return new \Twig_Node_Expression_Filter( + $node, + new \Twig_Node(array(new \Twig_Node_Expression_Constant('safe', $line), new \Twig_Node())), + $line + ); + } + + protected function output(Twig_NodeInterface $node, $line = -1) + { + return new \Twig_Node_Print($node, $line); + } + + protected function getNodeValues(array $values) + { + $nodes = array(); + foreach ($values as $value) { + if ($value instanceof \Twig_NodeInterface) { + $nodes[] = $value; + } + } + + return $nodes; + } + static public function parseGrammar($str, $main = true) { static $cursor; -- 1.7.2.5