2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Twig;
|
|
|
|
|
2019-03-27 13:15:17 -06:00
|
|
|
use Twig\Token as TwigToken;
|
|
|
|
use Twig\TokenParser\AbstractTokenParser as TwigTokenParser;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2021-03-10 15:02:53 -06:00
|
|
|
* @package winter\wn-cms-module
|
2014-05-14 23:24:20 +10:00
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
2019-03-27 13:15:17 -06:00
|
|
|
class PutTokenParser extends TwigTokenParser
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Parses a token and returns a node.
|
|
|
|
*
|
2019-03-27 13:15:17 -06:00
|
|
|
* @param TwigToken $token A TwigToken instance
|
|
|
|
* @return Twig\Node\Node A Twig\Node\Node instance
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2019-03-27 13:15:17 -06:00
|
|
|
public function parse(TwigToken $token)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-09-06 17:10:52 +10:00
|
|
|
$lineno = $token->getLine();
|
2014-05-14 23:24:20 +10:00
|
|
|
$stream = $this->parser->getStream();
|
2019-03-27 13:15:17 -06:00
|
|
|
$name = $stream->expect(TwigToken::NAME_TYPE)->getValue();
|
|
|
|
$stream->expect(TwigToken::BLOCK_END_TYPE);
|
2014-05-14 23:24:20 +10:00
|
|
|
$body = $this->parser->subparse([$this, 'decidePutEnd'], true);
|
2014-09-06 17:10:52 +10:00
|
|
|
|
|
|
|
$endType = null;
|
2019-03-27 13:15:17 -06:00
|
|
|
if ($token = $stream->nextIf(TwigToken::NAME_TYPE)) {
|
2014-09-06 17:10:52 +10:00
|
|
|
$endType = $token->getValue();
|
|
|
|
}
|
|
|
|
|
2019-03-27 13:15:17 -06:00
|
|
|
$stream->expect(TwigToken::BLOCK_END_TYPE);
|
2014-05-14 23:24:20 +10:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-27 13:15:17 -06:00
|
|
|
public function decidePutEnd(TwigToken $token)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|