3

string がありPicture > 1000 words、HTML エスケープされて保存されますPicture > 1000 words。元の文字列を URL エンコードして取得する必要がありますPicture%20%3E%201000%20words

さまざまなフィルターのシーケンスを試しましたが、どれも望ましい結果をもたらしません。

{# title = "Picture > 1000 words" #}

{{ title | url_encode(true)  }}
{{ title | raw | url_encode(true)  }}
{{ title | url_encode(true) | raw  }}

結果は 3 つのケースすべてで同じですPicture%20%26gt%3B%201000%20words。Twig が既にエスケープされたテキストをエンコードするのを回避し、望ましい結果を得るにはどうすればよいですか?

4

1 に答える 1

5

これを取得するにPicture%20%3E%201000%20wordsは、html エンティティのない生の文字列が必要です

したがって、これは機能するはずです:

{% set title = "Picture > 1000 words" %}

{{ title | url_encode(true)  }}

テンプレート内のエンティティを本当にデコードする必要がある場合は、その目的のためにカスタム フィルターを登録できます。

$filter = new Twig_SimpleFilter('decode_entities', function ($string) {
   return html_entity_decode($string);
});

// then connect it with environment

$twig = new Twig_Environment($loader);
$twig->addFilter($filter);

そして、次のように使用します。

{{ title | decode_entities | url_encode(true) }}

編集

あなたの例を最新の上流の小枝で試してみたところ、これは期待どおりに機能します。

{{ title | raw | url_encode(true) }}

あなたの問題は、間違った文字列エンティティです

于 2013-10-16T11:45:06.340 に答える