[TwigBundle] updated the bundle according to the latest version of Twig

This commit is contained in:
Fabien Potencier 2010-06-08 16:50:16 +02:00
parent 395081debe
commit b86880da5e
9 changed files with 112 additions and 331 deletions

View File

@ -3,10 +3,7 @@
namespace Symfony\Framework\TwigBundle\Extension;
use Symfony\Components\Templating\Engine;
use Symfony\Framework\TwigBundle\TokenParser\StylesheetTokenParser;
use Symfony\Framework\TwigBundle\TokenParser\StylesheetsTokenParser;
use Symfony\Framework\TwigBundle\TokenParser\RouteTokenParser;
use Symfony\Framework\TwigBundle\TokenParser\RenderTokenParser;
use Symfony\Framework\TwigBundle\TokenParser\HelperTokenParser;
/*
* This file is part of the Symfony package.
@ -33,12 +30,23 @@ class Helpers extends \Twig_Extension
public function getTokenParsers()
{
return array(
new JavascriptTokenParser(),
new JavascriptsTokenParser(),
new StylesheetTokenParser(),
new StylesheetsTokenParser(),
new RouteTokenParser(),
new RenderTokenParser(),
// {% javascript 'bundles/blog/js/blog.js' %}
new HelperTokenParser('stylesheet', '<js> [with <arguments:array>]', 'javascripts', 'add'),
// {% javascripts %}
new HelperTokenParser('stylesheets', '', 'stylesheets', 'render'),
// {% stylesheet 'bundles/blog/css/blog.css' with ['media': 'screen'] %}
new HelperTokenParser('stylesheet', '<css> [with <arguments:array>]', 'stylesheets', 'add'),
// {% stylesheets %}
new HelperTokenParser('stylesheets', '', 'stylesheets', 'render'),
// {% route 'blog_post' with ['id': post.id] %}
new HelperTokenParser('route', '<route> [with <arguments:array>]', 'router', 'generate'),
// {% render 'BlogBundle:Post:list' with ['path': ['limit': 2], 'alt': 'BlogBundle:Post:error'] %}
new HelperTokenParser('render', '<template> [with <arguments:array>]', 'actions', 'render'),
);
}

View File

@ -1,53 +0,0 @@
<?php
namespace Symfony\Framework\TwigBundle\Node;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class HelperNode extends \Twig_Node
{
public function __construct($name, $method, \Twig_Node $arguments = null, $echo, $lineno, $tag = null)
{
parent::__construct(array('arguments' => $arguments), array('name' => $name, 'method' => $method, 'echo' => $echo), $lineno, $tag);
}
public function compile($compiler)
{
$compiler
->addDebugInfo($this)
->write('')
;
if ($this['echo']) {
$compiler->raw('echo ');
}
$compiler->raw('$context[\'_view\']->'.$this['name'].'->'.$this['method'].'(');
if (null !== $this->arguments) {
$count = count($this->arguments);
foreach ($this->arguments as $i => $node) {
$compiler->subcompile($node);
if ($i !== $count - 1) {
$compiler->raw(', ');
}
}
}
$compiler->raw(");\n");
}
}

View File

@ -0,0 +1,94 @@
<?php
namespace Symfony\Framework\TwigBundle\TokenParser;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Wrapper for Symfony helpers.
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class HelperTokenParser extends \Twig_SimpleTokenParser
{
protected $helper;
protected $method;
protected $grammar;
protected $tag;
public function __construct($tag = null, $grammar = null, $helper = null, $method = null)
{
$this->tag = $tag;
$this->grammar = $grammar;
$this->helper = $helper;
$this->method = $method;
}
/**
* Gets the tag name associated with this token parser.
*
* @param string The tag name
*/
public function getTag()
{
return $this->tag;
}
/**
* Gets the grammar as an object or as a string.
*
* @return string|Twig_Grammar A Twig_Grammar instance or a string
*/
protected function getGrammar()
{
return $this->grammar;
}
protected function getNode(array $values, $line)
{
$helper = new \Twig_Node_Expression_GetAttr(
new \Twig_Node_Expression_Name('_view', $line),
new \Twig_Node_Expression_Constant($this->helper, $line),
new \Twig_Node(),
\Twig_Node_Expression_GetAttr::TYPE_ANY,
$line
);
$call = new \Twig_Node_Expression_GetAttr(
$helper,
new \Twig_Node_Expression_Constant($this->method, $line),
new \Twig_Node($this->getArguments($values)),
\Twig_Node_Expression_GetAttr::TYPE_METHOD,
$line
);
$safe = new \Twig_Node_Expression_Filter(
$call,
new \Twig_Node(array(new \Twig_Node_Expression_Constant('safe', $line), new \Twig_Node())),
$line
);
return new \Twig_Node_Print($safe, $line);
}
protected function getArguments(array $values)
{
$arguments = array();
foreach ($values as $value) {
if ($value instanceof \Twig_NodeInterface) {
$arguments[] = $value;
}
}
return $arguments;
}
}

View File

@ -1,48 +0,0 @@
<?php
namespace Symfony\Framework\TwigBundle\TokenParser;
use Symfony\Framework\TwigBundle\Node\HelperNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Wrapper for the javascripts helper add() method.
*
* {% javascript 'bundles/blog/js/blog.js' %}
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class JavascriptTokenParser extends \Twig_TokenParser
{
public function parse(\Twig_Token $token)
{
$stream = $this->parser->getStream();
$nodes = array($this->parser->getExpressionParser()->parseExpression());
if ($stream->test(\Twig_Token::NAME_TYPE, 'with')) {
$stream->next();
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new HelperNode('javascripts', 'add', new \Twig_Node($nodes), false, $token->getLine(), $this->getTag());
}
public function getTag()
{
return 'javascript';
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace Symfony\Framework\TwigBundle\TokenParser;
use Symfony\Framework\TwigBundle\Node\HelperNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Wrapper for the javascripts helper output() method.
*
* {% javascripts %}
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class JavascriptsTokenParser extends \Twig_TokenParser
{
public function parse(\Twig_Token $token)
{
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
return new HelperNode('javascripts', 'render', null, true, $token->getLine(), $this->getTag());
}
public function getTag()
{
return 'javascripts';
}
}

View File

@ -1,48 +0,0 @@
<?php
namespace Symfony\Framework\TwigBundle\TokenParser;
use Symfony\Framework\TwigBundle\Node\HelperNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Wrapper for the actions helper render() method.
*
* {% render 'BlogBundle:Post:list' with ['path': ['limit': 2], 'standalone': false, 'alt': 'BlogBundle:Post:error'] %}
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class RenderTokenParser extends \Twig_TokenParser
{
public function parse(\Twig_Token $token)
{
$nodes = array();
$lineno = $token->getLine();
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) {
$this->parser->getStream()->expect(\Twig_Token::NAME_TYPE, 'with');
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
}
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
return new HelperNode('actions', 'render', new \Twig_Node($nodes), true, $lineno, $this->getTag());
}
public function getTag()
{
return 'render';
}
}

View File

@ -1,48 +0,0 @@
<?php
namespace Symfony\Framework\TwigBundle\TokenParser;
use Symfony\Framework\TwigBundle\Node\HelperNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Wrapper for the route helper generate() method.
*
* {% route 'blog_post' with ['id': post.id] %}
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class RouteTokenParser extends \Twig_TokenParser
{
public function parse(\Twig_Token $token)
{
$nodes = array();
$lineno = $token->getLine();
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) {
$this->parser->getStream()->expect(\Twig_Token::NAME_TYPE, 'with');
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
}
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
return new HelperNode('router', 'generate', new \Twig_Node($nodes), true, $lineno, $this->getTag());
}
public function getTag()
{
return 'route';
}
}

View File

@ -1,48 +0,0 @@
<?php
namespace Symfony\Framework\TwigBundle\TokenParser;
use Symfony\Framework\TwigBundle\Node\HelperNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Wrapper for the stylesheets helper add() method.
*
* {% stylesheet 'bundles/blog/css/blog.css' with ['media': 'screen'] %}
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class StylesheetTokenParser extends \Twig_TokenParser
{
public function parse(\Twig_Token $token)
{
$stream = $this->parser->getStream();
$nodes = array($this->parser->getExpressionParser()->parseExpression());
if ($stream->test(\Twig_Token::NAME_TYPE, 'with')) {
$stream->next();
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new HelperNode('stylesheets', 'add', new \Twig_Node($nodes), false, $token->getLine(), $this->getTag());
}
public function getTag()
{
return 'stylesheet';
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace Symfony\Framework\TwigBundle\TokenParser;
use Symfony\Framework\TwigBundle\Node\HelperNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Wrapper for the stylesheets helper output() method.
*
* {% stylesheets %}
*
* @package Symfony
* @subpackage Framework_TwigBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class StylesheetsTokenParser extends \Twig_TokenParser
{
public function parse(\Twig_Token $token)
{
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
return new HelperNode('stylesheets', 'render', null, true, $token->getLine(), $this->getTag());
}
public function getTag()
{
return 'stylesheets';
}
}