From cdc580101597b149f5404873ba949087c763bd27 Mon Sep 17 00:00:00 2001 From: Ludovico Magnocavallo Date: Sun, 16 Sep 2012 15:21:54 +0200 Subject: [PATCH] elseif support --- h2o/parser.php | 6 +++--- h2o/tags.php | 51 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/h2o/parser.php b/h2o/parser.php index 15b819a..40deda9 100644 --- a/h2o/parser.php +++ b/h2o/parser.php @@ -81,11 +81,11 @@ function &parse() { $node = new CommentNode($token->content); break; case 'block' : - if (in_array($token->content, $until)) { - $this->token = $token; + @list($name, $args) = preg_split('/\s+/',$token->content, 2); + if (in_array($name, $until)) { + $this->token = $token; return $nodelist; } - @list($name, $args) = preg_split('/\s+/',$token->content, 2); $node = H2o::createTag($name, $args, $this, $token->position); $this->token = $token; } diff --git a/h2o/tags.php b/h2o/tags.php index c207ce2..0d455a0 100644 --- a/h2o/tags.php +++ b/h2o/tags.php @@ -60,18 +60,31 @@ function render($context, $stream) { class If_Tag extends H2o_Node { private $body; + private $elseif = array(); private $else; private $negate; function __construct($argstring, $parser, $position = 0) { if (preg_match('/\s(and|or)\s/', $argstring)) throw new TemplateSyntaxError('H2o doesn\'t support multiple expressions'); - - $this->body = $parser->parse('endif', 'else'); - if ($parser->token->content === 'else') - $this->else = $parser->parse('endif'); - + $this->body = $parser->parse('elseif', 'endif', 'else'); + + @list($name, $args) = preg_split('/\s+/', $parser->token->content, 2); + + while ($name !== 'endif') { + if ($name == 'else') { + $this->else = $parser->parse('endif'); + } else if ($name === 'elseif') { + $this->elseif[] = array( + 'body' => $parser->parse('elseif', 'else', 'endif'), + 'args' => $args + ); + } else { + break; + } + @list($name, $args) = preg_split('/\s+/',$parser->token->content, 2); + } $this->args = H2o_Parser::parseArguments($argstring); $first = current($this->args); @@ -82,15 +95,33 @@ function __construct($argstring, $parser, $position = 0) { } function render($context, $stream) { - if ($this->test($context)) + if ($this->test($context)) { $this->body->render($context, $stream); - elseif ($this->else) + return; + } elseif ($this->elseif) { + foreach ($this->elseif as $k=>$v) { + $args = H2o_Parser::parseArguments($v['args']); + $negate = false; + $first = current($args); + if (isset($first['operator']) && $first['operator'] === 'not') { + array_shift($args); + $negate = true; + } + if ($this->test($context, $args, $negate)) { + $v['body']->render($context, $stream); + return; + } + } + } elseif ($this->else) { $this->else->render($context, $stream); + } } - function test($context) { - $test = Evaluator::exec($this->args, $context); - return $this->negate? !$test : $test; + function test($context, $args=null, $negate=null) { + $args = is_null($args) ? $this->args : $args; + $negate = is_null($negate) ? $this->negate : $negate; + $test = Evaluator::exec($args, $context); + return $negate? !$test : $test; } }