1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- /**
- * @package Grav.Common.Twig
- *
- * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Common\Twig\TokenParser;
- use Grav\Common\Twig\Node\TwigNodeTryCatch;
- /**
- * Handles try/catch in template file.
- *
- * <pre>
- * {% try %}
- * <li>{{ user.get('name') }}</li>
- * {% catch %}
- * {{ e.message }}
- * {% endcatch %}
- * </pre>
- */
- class TwigTokenParserTryCatch extends \Twig_TokenParser
- {
- /**
- * Parses a token and returns a node.
- *
- * @param \Twig_Token $token A Twig_Token instance
- *
- * @return \Twig_Node A Twig_Node instance
- */
- public function parse(\Twig_Token $token)
- {
- $lineno = $token->getLine();
- $stream = $this->parser->getStream();
- $stream->expect(\Twig_Token::BLOCK_END_TYPE);
- $try = $this->parser->subparse([$this, 'decideCatch']);
- $stream->next();
- $stream->expect(\Twig_Token::BLOCK_END_TYPE);
- $catch = $this->parser->subparse([$this, 'decideEnd']);
- $stream->next();
- $stream->expect(\Twig_Token::BLOCK_END_TYPE);
- return new TwigNodeTryCatch($try, $catch, $lineno, $this->getTag());
- }
- public function decideCatch(\Twig_Token $token)
- {
- return $token->test(array('catch'));
- }
- public function decideEnd(\Twig_Token $token)
- {
- return $token->test(array('endtry')) || $token->test(array('endcatch'));
- }
- /**
- * Gets the tag name associated with this token parser.
- *
- * @return string The tag name
- */
- public function getTag()
- {
- return 'try';
- }
- }
|