Symfony2 and zclip: apostrophe turn into & # 0 3 9 ;

只谈情不闲聊 提交于 2019-12-13 18:25:13

问题


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...


回答1:


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




回答2:


Another way

string_name = '{{ string_name|e('js')|raw }}';


来源:https://stackoverflow.com/questions/10631183/symfony2-and-zclip-apostrophe-turn-into-0-3-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!