[英]How do I extract a plain text parsed from HTML in Twig?
I am making a kind of blog project with Symfony2
. 我正在用Symfony2
做一个博客项目。
I create the content of my articles with IvoryCKEditorBundle
and then display them with {{ article.content|raw }}
in Twig. 我使用IvoryCKEditorBundle
创建文章的内容,然后在Twig中使用{{ article.content|raw }}
显示它们。
I want to display on my homepage the first lines of the article: To do this, I need to parse the HTML code and extract strings in it. 我想在首页上显示该文章的第一行:为此,我需要解析HTML代码并提取其中的字符串。
How do I extract plain text from HTML, in my Twig template? 如何在Twig模板中从HTML提取纯文本?
Twig offers a bunch of filters that you can use. Twig提供了一堆可以使用的过滤器。 If you want to display only the first line, you can slice your string after a certain number of characters. 如果您只想显示第一行,则可以在一定数量的字符后切字符串。 You should look into the documentation of Twig. 您应该查看Twig的文档。 There is also another filter to strip the tags if you need to do it before slicing your string. 如果需要在切片字符串之前进行剥离,还可以使用另一个过滤器来剥离标签。
{{ '12345'|slice(1, 2) }}
{# outputs 23 #}
http://twig.sensiolabs.org/doc/filters/slice.html http://twig.sensiolabs.org/doc/filters/slice.html
You can get plain text using "striptags" filter then apply "truncate" filter of twig to get portion of your post. 您可以使用“ striptags”过滤器获取纯文本,然后应用嫩枝的“ truncate”过滤器以获取部分帖子。 for more detail please read my answer https://stackoverflow.com/a/29295738/998686 有关更多详细信息,请阅读我的回答https://stackoverflow.com/a/29295738/998686
If you need extra functionality I guess you need to write new twig extension and put any custom logic there. 如果您需要其他功能,我想您需要编写新的树枝扩展并将任何自定义逻辑放在此处。 It do not take much time but allows you to do what you want. 它不需要花费很多时间,但是可以让您做自己想做的事情。 Have a look at http://symfony.com/doc/current/cookbook/templating/twig_extension.html 看看http://symfony.com/doc/current/cookbook/templating/twig_extension.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.