4

パスから生成されたエスケープされていない URL を入力要素に入れる必要があります。

ルーティング.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 %}タグと結果の間に小枝コードを入れても同じです。

4

2 に答える 2

13

Twigには、そのurl_encodeフィルターと一致するurl_decodeフィルターが付属していないため、これを作成する必要があります。

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 }}"/>
于 2012-05-17T17:08:55.607 に答える
0

使用している場合:

'url_decode' => new \Twig_Function_Method($this, 'urlDecode') 

そしてエラーを受け取ります:

Error: addFilter() must implement interface Twig_FilterInterface, instance of Twig_Function_Method given 

交換:

new \Twig_Function_Method($this, 'urlDecode')" 

と:

new \Twig_Filter_Method($this, 'urlDecode')"

一番

于 2012-06-07T09:24:41.970 に答える