twig replace和raw工作不正确

时间:2015-02-24 16:30:52

标签: twig

我有枝条代码

                <script type="text/javascript">
                    $(document).ready(function() {
                        $('#{{ setting.name|e }}').editable({
                            {% if setting.options is not empty %}
                            source: {{ setting.options|raw }},
                            {% endif %}
                            value: '{{ setting.value|replace({"\n": "", "\r\n": "", "\t": "", "\n\r": ""})|raw}}',
                            placement: 'bottom',
                            pk: '{{ setting.id }}',
                            url: '{{ siteUrl('/admin/settings/edit/' ~ setting.id ) }}',
                            ajaxOptions: {type: 'post'}
                        });
                    });
                </script>

此代码

value: '{{ setting.value|replace({"\n": "", "\r\n": "", "\t": "", "\n\r": ""})raw}}'
在textarea

中输入html标签时,

无法正常工作

value: '    &lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;'

value: '    <div id="disqus_thread"></div>;

我如何解决问题?

1 个答案:

答案 0 :(得分:2)

设置:

value: '{{ setting.value|replace({"\n": "", "\r\n": "", "\t": "", "\n\r": ""})|raw}}'

您忘记在|之前设置raw

编辑: 这是工作。我设定了这个:

    {% set val = "sdfg sdfg sdfg
s dfg
<p>

 sd fg
 </p>" %}
{{ val }}

输出:

sdfg sdfg sdfg
s dfg
&lt;p&gt;

 sd fg
 &lt;/p&gt;

集:

{% set val = "sdfg sdfg sdfg
s dfg
<p>

 sd fg
 </p>" %}
{{ val|replace({"\n": "", "\r\n": "", "\t": "", "\n\r": ""})|raw }}

输出:

sdfg sdfg sdfgs dfg<p> sd fg </p>