moved integration between Routing and Twig to a Symfony Bridge
This commit is contained in:
parent
abb99e9469
commit
3e5bd67dac
62
src/Symfony/Bridge/Twig/Extension/RoutingExtension.php
Normal file
62
src/Symfony/Bridge/Twig/Extension/RoutingExtension.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bridge\Twig\Extension;
|
||||
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
|
||||
/**
|
||||
* Provides integration of the Routing component with Twig.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class RoutingExtension extends \Twig_Extension
|
||||
{
|
||||
private $router;
|
||||
|
||||
public function __construct(RouterInterface $router)
|
||||
{
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of functions to add to the existing list.
|
||||
*
|
||||
* @return array An array of functions
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
'url' => new \Twig_Function_Method($this, 'getUrl'),
|
||||
'path' => new \Twig_Function_Method($this, 'getPath'),
|
||||
);
|
||||
}
|
||||
|
||||
public function getPath($name, array $parameters = array())
|
||||
{
|
||||
return $this->router->generate($name, $parameters, false);
|
||||
}
|
||||
|
||||
public function getUrl($name, array $parameters = array())
|
||||
{
|
||||
return $this->router->generate($name, $parameters, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return string The extension name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'routing';
|
||||
}
|
||||
}
|
@ -66,22 +66,10 @@ class TemplatingExtension extends \Twig_Extension
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
'url' => new \Twig_Function_Method($this, 'getUrl'),
|
||||
'path' => new \Twig_Function_Method($this, 'getPath'),
|
||||
'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
|
||||
);
|
||||
}
|
||||
|
||||
public function getPath($name, array $parameters = array())
|
||||
{
|
||||
return $this->container->get('router')->generate($name, $parameters, false);
|
||||
}
|
||||
|
||||
public function getUrl($name, array $parameters = array())
|
||||
{
|
||||
return $this->container->get('router')->generate($name, $parameters, true);
|
||||
}
|
||||
|
||||
public function getAssetUrl($location, $packageName = null)
|
||||
{
|
||||
return $this->container->get('templating.helper.assets')->getUrl($location, $packageName);
|
||||
|
@ -43,6 +43,11 @@
|
||||
<argument type="service" id="service_container" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.routing" class="Symfony\Bridge\Twig\Extension\RoutingExtension" public="false">
|
||||
<tag name="twig.extension" />
|
||||
<argument type="service" id="router" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.form" class="Symfony\Bundle\TwigBundle\Extension\FormExtension" public="false">
|
||||
<tag name="twig.extension" />
|
||||
<argument>%twig.form.resources%</argument>
|
||||
|
Reference in New Issue
Block a user