Symfony2 和 zclip:撇号变成 &# 0 3 9 ; [英] Symfony2 and zclip: apostrophe turn into & # 0 3 9 ;

查看:31
本文介绍了Symfony2 和 zclip:撇号变成 &# 0 3 9 ;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jquery.zclip 和 Symfony2 框架将文本复制到客户端的剪贴板.它工作正常,但撇号转换为 &# 0 3 9 ;(没有空格)

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)

问题显然来自将我的撇号转换为 html 实体的 Twig 模板引擎.

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

我想找到一种解决方案来防止 Twig 这样做.或者在调用 zclip 之前将它们转回撇号?

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

你会怎么做?

这是我的 jquery/twig 代码:

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>

此代码然后变为:

$(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.');
    },
});

我尝试了一些也不起作用的东西:

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

推荐答案

使用 raw 如果您认为变量安全,则过滤:

Use raw filter if you consider your variable safe:

{{ var|raw }}

您还可以将 自动转义策略 更改为 javascript(可能更好选项):

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#转义扩展

这篇关于Symfony2 和 zclip:撇号变成 &amp;# 0 3 9 ;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆