Wearybands
Wearybands

Reputation: 2455

php function inside twig template

I have a twig template in which i render some information taking from the database. The length of the information is quite large and it does not fit in the space provided for it. I want to use substr function of php inside my twig template.

The index.html.twig contains

 <span>{{ patent.description }}</span>

The description is very long I want to display the first 80 characters of the whole description.

In php I can use

 substr(patent.description,0,80)

Can anyone guide me how i can use this function inside my twig template?

Upvotes: 2

Views: 2518

Answers (1)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

The slice function does this,

<span>{{ patent.description|slice(0,80) }}</span>

The slice filter works as the array_slice PHP function for arrays and substr for strings. It was added in Twig 1.6.

Upvotes: 6

Related Questions