dongqiongjiong4740 2014-06-28 17:55 采纳率: 0%
浏览 69

如何从Twig中提取从HTML解析的纯文本?

I am making a kind of blog project with Symfony2.

I create the content of my articles with IvoryCKEditorBundle and then display them with {{ article.content|raw }}in Twig.

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.

How do I extract plain text from HTML, in my Twig template?

  • 写回答

5条回答 默认 最新

  • duandan1995 2014-06-28 18:05
    关注

    Twig offers a bunch of filters that you can use. 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. 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

    评论

报告相同问题?