fixed HelpersExtension (removed usage of the magic _view context attribute -- helpers should now work from macros)
This commit is contained in:
parent
eff1bdf50f
commit
3ce8ad1718
@ -3,6 +3,7 @@
|
|||||||
namespace Symfony\Bundle\TwigBundle\Extension;
|
namespace Symfony\Bundle\TwigBundle\Extension;
|
||||||
|
|
||||||
use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
|
use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the Symfony package.
|
* This file is part of the Symfony package.
|
||||||
@ -19,6 +20,18 @@ use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
|
|||||||
*/
|
*/
|
||||||
class HelpersExtension extends \Twig_Extension
|
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.
|
* Returns the token parser instance to add to the existing list.
|
||||||
*
|
*
|
||||||
@ -28,28 +41,28 @@ class HelpersExtension extends \Twig_Extension
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
// {% javascript 'bundles/blog/js/blog.js' %}
|
// {% 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 %}
|
// {% javascripts %}
|
||||||
new HelperTokenParser('javascripts', '', 'javascripts', 'render'),
|
new HelperTokenParser('javascripts', '', 'templating.helper.javascripts', 'render'),
|
||||||
|
|
||||||
// {% stylesheet 'bundles/blog/css/blog.css' with ['media': 'screen'] %}
|
// {% 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 %}
|
// {% stylesheets %}
|
||||||
new HelperTokenParser('stylesheets', '', 'stylesheets', 'render'),
|
new HelperTokenParser('stylesheets', '', 'templating.helper.stylesheets', 'render'),
|
||||||
|
|
||||||
// {% asset 'css/blog.css' %}
|
// {% 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] %}
|
// {% 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'] %}
|
// {% 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' %}
|
// {% flash 'notice' %}
|
||||||
new HelperTokenParser('flash', '<name>', 'session', 'flash'),
|
new HelperTokenParser('flash', '<name>', 'templating.helper.session', 'flash'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
|
|
||||||
<service id="twig.extension.helpers" class="Symfony\Bundle\TwigBundle\Extension\HelpersExtension">
|
<service id="twig.extension.helpers" class="Symfony\Bundle\TwigBundle\Extension\HelpersExtension">
|
||||||
<tag name="twig.extension" />
|
<tag name="twig.extension" />
|
||||||
|
<argument type="service" id="service_container" />
|
||||||
</service>
|
</service>
|
||||||
</services>
|
</services>
|
||||||
</container>
|
</container>
|
||||||
|
@ -56,7 +56,11 @@ class HelperTokenParser extends \Twig_SimpleTokenParser
|
|||||||
return $this->output(
|
return $this->output(
|
||||||
$this->markAsSafe(
|
$this->markAsSafe(
|
||||||
$this->call(
|
$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->method,
|
||||||
$this->getNodeValues($values)
|
$this->getNodeValues($values)
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user