Search code examples
symfonytwigtruncatetwig-extension

Twig truncate with read more link


I want to use the truncate filter of Twig. And it works fine.

I wanted to use as a third argument a link to read more about the article. But it does not work.

Here is the code:

{% set read_more_link %}
      <a href="{{ path('course_news_show', {news: entry.id}) }}">... read more</a>
{% endset %}

<p>{{ entry.description|truncate(150, true, "read_more_link") }}</p>

What comes out is this:

"Lorem ipsum dolor sit met, <a href="/course/news/44">... read more </a>"
                                

So, the link is displayed as text in the <p>. How can I solve this problem?


Solution

  • Try to use raw filter after your truncate filter:

    <p>{{ entry.description|truncate(150, true, "read_more_link")|raw }}</p>