In the core symfony / twig do not have a simple truncate-text-function, like substr() in PHP. Therefor you have to activate the Twig-Text-Extension:
1 2 3 4 5 6 |
// app/config/config.yml services: twig.extension.text: class: Twig_Extensions_Extension_Text tags: - { name: twig.extension } |
Now you can easily truncate as you like:
1 |
{{ var.long_description | truncate(50) }} |
If you want to preserve whole words, you have to set the 2nd parameter:
1 |
{{ var.long_description | truncate(50, true) }} |
With the 3rd parameter you can override the default seperator ‘…':
1 |
{{ var.long_description | truncate(50, true, '.......') }} |
Thanks Scott
Great thanks, this is exactly what i was looking for
I was trying to truncate that was being saved as HTML (using ckeditor) so looking at your tip this is what worked for me
{{var.long_description | truncate(200, true, ‘…’) |raw }}
I also want functionality as Shairyar Baig wants. Can you please help.
{{var.description | truncate(100, true, ‘…’) |raw }}