2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Twig;
|
|
|
|
|
|
|
|
use Twig_Token;
|
|
|
|
use Twig_TokenParser;
|
|
|
|
|
|
|
|
/**
|
2017-03-16 17:08:20 +11:00
|
|
|
* Parser for the `{% put %}` Twig tag.
|
2014-05-14 23:24:20 +10:00
|
|
|
*
|
2017-03-16 17:08:20 +11:00
|
|
|
* {% put head %}
|
|
|
|
* <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet"/>
|
|
|
|
* {% endput %}
|
2014-05-14 23:24:20 +10:00
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
2017-03-16 17:08:20 +11:00
|
|
|
* {% put head %}
|
|
|
|
* <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet"/>
|
|
|
|
* {% default %}
|
|
|
|
* {% endput %}
|
2014-05-14 23:24:20 +10:00
|
|
|
*
|
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class PutTokenParser extends Twig_TokenParser
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Parses a token and returns a node.
|
|
|
|
*
|
|
|
|
* @param Twig_Token $token A Twig_Token instance
|
2017-05-13 09:34:20 +10:00
|
|
|
* @return Twig_Node A Twig_Node instance
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function parse(Twig_Token $token)
|
|
|
|
{
|
2014-09-06 17:10:52 +10:00
|
|
|
$lineno = $token->getLine();
|
2014-05-14 23:24:20 +10:00
|
|
|
$stream = $this->parser->getStream();
|
|
|
|
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
|
|
|
|
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
|
|
|
$body = $this->parser->subparse([$this, 'decidePutEnd'], true);
|
2014-09-06 17:10:52 +10:00
|
|
|
|
|
|
|
$endType = null;
|
|
|
|
if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
|
|
|
|
$endType = $token->getValue();
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
|
|
|
|
2014-09-06 17:10:52 +10:00
|
|
|
return new PutNode($body, $name, $endType, $lineno, $this->getTag());
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
public function decidePutEnd(Twig_Token $token)
|
|
|
|
{
|
|
|
|
return $token->test('endput');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the tag name associated with this token parser.
|
|
|
|
*
|
|
|
|
* @return string The tag name
|
|
|
|
*/
|
|
|
|
public function getTag()
|
|
|
|
{
|
|
|
|
return 'put';
|
|
|
|
}
|
2014-10-11 01:42:04 +02:00
|
|
|
}
|