Limit String Twig
Try this :
{{ entity.description|striptags|slice(0, 40) }}
- the
striptags
filter will remove the HTML tags, this will avoid to cut a tag in 2, example of this base case:Text ... <img src="http://examp
- the
slice
filter will cut the text, keeping only the 40 first characters
Try with Truncate function:
First, you need to activated Text extension:
# app/config/config.yml services: twig.extension.text: class: Twig_Extensions_Extension_Text tags: - { name: twig.extension }
Then, you can call truncate()
helper within your Twig template as follow:
{{ variable.description | truncate(100, true) }}
I used this to truncate blog posts and show an ellipsis..
{{ post.excerpt|striptags|length > 100 ? post.excerpt|striptags|slice(0, 100) ~ '...' : post.excerpt|striptags }}
If the post excerpt length is greater than 100 characters then slice
it at the 100s character starting from the first one and append an '...'Otherwise show full text..