fixed HelpersExtension (removed usage of the magic _view context attribute -- helpers should now work from macros)

This commit is contained in:
Fabien Potencier 2010-10-01 20:29:05 +02:00
parent eff1bdf50f
commit 3ce8ad1718
3 changed files with 27 additions and 9 deletions

View File

@ -3,6 +3,7 @@
namespace Symfony\Bundle\TwigBundle\Extension;
use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
use Symfony\Component\DependencyInjection\ContainerInterface;
/*
* This file is part of the Symfony package.
@ -19,6 +20,18 @@ use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
*/
class HelpersExtension extends \Twig_Extension
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getContainer()
{
return $this->container;
}
/**
* Returns the token parser instance to add to the existing list.
*
@ -28,28 +41,28 @@ class HelpersExtension extends \Twig_Extension
{
return array(
// {% javascript 'bundles/blog/js/blog.js' %}
new HelperTokenParser('javascript', '<js> [with <arguments:array>]', 'javascripts', 'add'),
new HelperTokenParser('javascript', '<js> [with <arguments:array>]', 'templating.helper.javascripts', 'add'),
// {% javascripts %}
new HelperTokenParser('javascripts', '', 'javascripts', 'render'),
new HelperTokenParser('javascripts', '', 'templating.helper.javascripts', 'render'),
// {% stylesheet 'bundles/blog/css/blog.css' with ['media': 'screen'] %}
new HelperTokenParser('stylesheet', '<css> [with <arguments:array>]', 'stylesheets', 'add'),
new HelperTokenParser('stylesheet', '<css> [with <arguments:array>]', 'templating.helper.stylesheets', 'add'),
// {% stylesheets %}
new HelperTokenParser('stylesheets', '', 'stylesheets', 'render'),
new HelperTokenParser('stylesheets', '', 'templating.helper.stylesheets', 'render'),
// {% asset 'css/blog.css' %}
new HelperTokenParser('asset', '<location>', 'assets', 'getUrl'),
new HelperTokenParser('asset', '<location>', 'templating.helper.assets', 'getUrl'),
// {% route 'blog_post' with ['id': post.id] %}
new HelperTokenParser('route', '<route> [with <arguments:array>]', 'router', 'generate'),
new HelperTokenParser('route', '<route> [with <arguments:array>]', 'templating.helper.router', 'generate'),
// {% render 'BlogBundle:Post:list' with ['limit': 2], ['alt': 'BlogBundle:Post:error'] %}
new HelperTokenParser('render', '<template> [with <attributes:array>[, <options:array>]]', 'actions', 'render'),
new HelperTokenParser('render', '<template> [with <attributes:array>[, <options:array>]]', 'templating.helper.actions', 'render'),
// {% flash 'notice' %}
new HelperTokenParser('flash', '<name>', 'session', 'flash'),
new HelperTokenParser('flash', '<name>', 'templating.helper.session', 'flash'),
);
}

View File

@ -44,6 +44,7 @@
<service id="twig.extension.helpers" class="Symfony\Bundle\TwigBundle\Extension\HelpersExtension">
<tag name="twig.extension" />
<argument type="service" id="service_container" />
</service>
</services>
</container>

View File

@ -56,7 +56,11 @@ class HelperTokenParser extends \Twig_SimpleTokenParser
return $this->output(
$this->markAsSafe(
$this->call(
$this->getAttribute('_view', $this->helper),
$this->call(
$this->call(new \Twig_Node_Expression_ExtensionReference('symfony.helpers', 0), 'getContainer'),
'get',
array(new \Twig_Node_Expression_Constant($this->helper, 0))
),
$this->method,
$this->getNodeValues($values)
)