function IfTokenParser::parse
Overrides TokenParserInterface::parse
File
-
vendor/
twig/ twig/ src/ TokenParser/ IfTokenParser.php, line 36
Class
- IfTokenParser
- Tests a condition.
Namespace
Twig\TokenParserCode
public function parse(Token $token) : Node {
$lineno = $token->getLine();
$expr = $this->parser
->getExpressionParser()
->parseExpression();
$stream = $this->parser
->getStream();
$stream->expect(Token::BLOCK_END_TYPE);
$body = $this->parser
->subparse([
$this,
'decideIfFork',
]);
$tests = [
$expr,
$body,
];
$else = null;
$end = false;
while (!$end) {
switch ($stream->next()
->getValue()) {
case 'else':
$stream->expect(Token::BLOCK_END_TYPE);
$else = $this->parser
->subparse([
$this,
'decideIfEnd',
]);
break;
case 'elseif':
$expr = $this->parser
->getExpressionParser()
->parseExpression();
$stream->expect(Token::BLOCK_END_TYPE);
$body = $this->parser
->subparse([
$this,
'decideIfFork',
]);
$tests[] = $expr;
$tests[] = $body;
break;
case 'endif':
$end = true;
break;
default:
throw new SyntaxError(\sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d).', $lineno), $stream->getCurrent()
->getLine(), $stream->getSourceContext());
}
}
$stream->expect(Token::BLOCK_END_TYPE);
return new IfNode(new Nodes($tests), $else, $lineno);
}