Twig 没有附带 url_decode 过滤器来匹配它的url_encode one,所以你需要编写它。
在src/Your/Bundle/Twig/Extension/YourExtension.php
<?php
namespace Your\Bundle\Twig\Extension;
class YourExtension extends \Twig_Extension
{
/**
* {@inheritdoc}
*/
public function getFilters()
{
return array(
'url_decode' => new \Twig_Filter_Method($this, 'urlDecode')
);
}
/**
* URL Decode a string
*
* @param string $url
*
* @return string The decoded URL
*/
public function urlDecode($url)
{
return urldecode($url);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'your_extension';
}
}
然后将其添加到app/config/config.yml中的服务配置中
services:
your.twig.extension:
class: Your\Bundle\Twig\Extension\YourExtension
tags:
- { name: twig.extension }
然后使用它!
<input id="deleteUrl" value="{{ path('profile_delete', {id: '$'})|url_decode }}"/>