Symfony2 Twig停止逃离路径

kor*_*ral 4 symfony twig

我需要将path生成的非转义URL放入input元素.

使用routing.yml

profile_delete:
  pattern: /student_usun/{id}
  defaults: { _controller: YyyXXXBundle:Profile:delete }
Run Code Online (Sandbox Code Playgroud)

list.html.twig

<input id="deleteUrl" value="{{ path('profile_delete', {id: '$'}) }}"/>
Run Code Online (Sandbox Code Playgroud)

结果是:

<input id="deleteUrl" value="/student_usun/%24"/>
Run Code Online (Sandbox Code Playgroud)

我尝试|raw过滤器,并在{% autoescape false %}标签之间放置twig代码,结果仍然相同.

Pet*_*ley 13

Twig没有url_decode过滤器来匹配其url_encode,所以你需要编写它.

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';
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将其添加到app/config/config.yml中的服务配置中

services:
    your.twig.extension:
        class: Your\Bundle\Twig\Extension\YourExtension
        tags:
            -  { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)

然后使用它!

<input id="deleteUrl" value="{{ path('profile_delete', {id: '$'})|url_decode }}"/>
Run Code Online (Sandbox Code Playgroud)