Symfony2 Twig 停止转义路径 [英] Symfony2 Twig stop escaping path

查看:24
本文介绍了Symfony2 Twig 停止转义路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将路径生成的未转义 URL 放入输入元素中.

I need to put unescaped URL generated from path into input element.

routing.yml

profile_delete:
  pattern: /student_usun/{id}
  defaults: { _controller: YyyXXXBundle:Profile:delete }

list.html.twig

<input id="deleteUrl" value="{{ path('profile_delete', {id: '$'}) }}"/>

结果是:

<input id="deleteUrl" value="/student_usun/%24"/>

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

I tried |raw filter and also put twig code between {% autoescape false %} tag and result is still the same.

推荐答案

Twig 没有提供 url_decode 过滤器来匹配它的 url_encode one,因此您需要编写它.

Twig doesn't come with a url_decode filter to match its url_encode one, so you'll need to write it.

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 }}"/>

这篇关于Symfony2 Twig 停止转义路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆