Twig:url编码两次,同时将其传递给Twitter分享按钮

I'm developing a simple page with Symfony2, using Twig as template engine.

I have a list of urls, and I'd like to add the Twitter share button for each url. What I do is a simple cycle on the urls array, and the dinaycally set the url for every Twitter button inside the cycle. It looks like that twig encodes the url at first, and the Twitter script encodes it again. So The Twitter share count doesn't match. The code (inside the cycle) is the following, there is another part of Twitter code at the end of the page:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="{{ s.url }}">Tweet</a>

The url I get on the rendered page is: http%253A%252F%252Fwww.example.com%252F (two encoding pass) instead of http%3A%2F%2Fwww.example.com%2F (one encoding pass, correct). It looks like the % is encoded again to %25.

And this doesn't make Twitter count work, because it consider those two as different urls.

I also tried to use some filters, e.g. {{ s.url|raw }}, but it didn't work.

So my question is: how to avoid this? Is there a way to tell twig (or twitter) to not encode the url?

You can always turn autoescaping off in Twig by using the {% autoescape false %} declaration before the code you want to leave raw. This will leave any strings you output unescaped, and thus your URL will not be escaped twice. Make sure you turn autoescaping back on with {% endautoescape %}

{% autoescape false %}
    <a href="https://twitter.com/share" class="twitter-share-button" data-url="{{ s.url }}">Tweet</a>
{% endautoescape %}

Full Twig HTML Escaping Documentation

An old post but looks like you can use the "raw" filter now. This should do:

{{ s.url|raw }}

http://twig.sensiolabs.org/doc/api.html#escaper-extension