1

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

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

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

你会怎么做?

这是我的 jquery/twig 代码:

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

编辑:我尝试了更多但也不起作用的东西:

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

但我仍然得到代码而不是撇号......

4

2 回答 2

1

如果您认为变量安全,请使用原始过滤器:

{{ var|raw }}

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

{% 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

于 2012-05-17T07:42:04.687 回答
0

其他方式

string_name = '{{ string_name|e('js')|raw }}';
于 2014-02-11T18:38:41.163 回答