树枝striptags和html特殊字符 [英] twig striptags and html special chars

查看:95
本文介绍了树枝striptags和html特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用树枝渲染视图,并且正在使用striptags过滤器删除html标签. 但是,由于整个元素都被"包围,因此html特殊字符现在呈现为文本. 在仍然使用striptags函数的情况下,如何剥离特殊字符或呈现它们?

I am using twig to render a view and I am using the striptags filter to remove html tags. However, html special chars are now rendered as text as the whole element is surrounded by "". How can I either strip special chars or render them, while still using the striptags function ?

示例:

{{ organization.content|striptags(" >")|truncate(200, '...') }}

{{ organization.content|striptags|truncate(200, '...') }}

输出:

"QUI SOMMES NOUS ? > NOS LOCAUXNOS LOCAUXDepuis 1995,  Ce lieu chargé d’histoire et de tradition s’inscrit dans les valeurs"

推荐答案

如果可以帮助其他人,这是我的解决方案

If it could help someone else, here is my solution

{{ organization.content|striptags|convert_encoding('UTF-8', 'HTML-ENTITIES') }}

您还可以添加修剪过滤器以删除前后的空格. 然后,您截断或切片您的组织.

You can also add a trim filter to remove spaces before and after. And then, you truncate or slice your organization.content

编辑2017年11月

如果您想将"\ n"换行符与截断符结合使用,可以执行

If you want to keep the "\n" break lines combined with a truncate, you can do

{{ organization.content|striptags|truncate(140, true, '...')|raw|nl2br }}

这篇关于树枝striptags和html特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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