Question

I'm using jquery.zclip and Symfony2 framework to copy text to the client's clipboard. It works fine but for the fact that the apostrophe's transform into & # 0 3 9 ; (without the spaces)

The problem apparently comes from Twig templating engine that transforms my apostrophes into their html entities.

I would like to find a solution to prevent Twig doing this. Or maybe turn them back to apostrophes before calling zclip?

How would you do that?

Here is my jquery/twig code:

<script>
    $(document).ready(function(){
        $('img#copy-description').zclip({
            path:"{{ asset('bundles/yopyourownpoet/flash/ZeroClipboard.swf') }}",
            copy:"{{ introLine1 }}{{ introLine2 }}{{ introLine3 }}{{ introLine4 }}{{ introLine5 }}",
            afterCopy:function(){
                alert('The poem has been copied to the clipboard.');
            },
        });
    });

</script>

This code then becomes:

$(document).ready(function(){
    $('img#copy-description').zclip({
    path:"/yourownpoet/web/bundles/yopyourownpoet/flash/ZeroClipboard.swf",
    copy:"Franz, hope Boston treats you wellDaddy, I have a story to tellIf you do not mindI&#039;ll promise it&#039;s kindLike the sweet ringing of a bell",
    afterCopy:function(){
    alert('The poem has been copied to the clipboard.');
    },
});

EDIT: I tried something more which doesn't work neither:

    function escape(string)
    {
        return string.replace("&#039;", "'");
    }

        $('img#copy-description').zclip({
            path:"{{ asset('bundles/yopyourownpoet/flash/ZeroClipboard.swf') }}",
            copy: escape("{{ introLine1 }}")+"\n"+escape("{{ introLine2 }}")+"\n"+escape("{{ introLine3 }}")+"\n"+escape("{{ introLine4 }}")+"\n"+escape("{{ introLine5 }}"),
            afterCopy:function(){
                alert('The poem has been copied to the clipboard.');
            },
        });

But I still get the code instead of apostrophe...

Was it helpful?

Solution

Use raw filter if you consider your variable safe:

{{ var|raw }}

You can also change the autoescaping strategy to javascript (probably better option):

{% autoescape true js %}
<script>
    $(document).ready(function(){
        $('img#copy-description').zclip({
            path:"{{ asset('bundles/yopyourownpoet/flash/ZeroClipboard.swf') }}",
            copy:"{{ introLine1 }}{{ introLine2 }}{{ introLine3 }}{{ introLine4 }}{{ introLine5 }}",
            afterCopy:function(){
                alert('The poem has been copied to the clipboard.');
            },
        });
    });
</script>
{% endautoescape %}

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

OTHER TIPS

Another way

string_name = '{{ string_name|e('js')|raw }}';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top