Truncate string in Laravel blade templates
In Laravel 4 & 5 (up to 5.7), you can use str_limit
, which limits the number of characters in a string.
While in Laravel 5.8 up, you can use the Str::limit
helper.
//For Laravel 4 to Laravel 5.5{{ str_limit($string, $limit = 150, $end = '...') }}//For Laravel 5.5 upwards{{ \Illuminate\Support\Str::limit($string, 150, $end='...') }}
For more Laravel helper functions http://laravel.com/docs/helpers#strings
Laravel 4 has Str::limit
which will truncate to the exact number of characters, and also Str::words
which will truncate on word boundary.
Check out:
Edit: This answer is was posted during the Laravel 4 beta, when Str class did not exist. There is now a better way to do it in Laravel 4 - which is Dustin's answer below. I cannot delete this answer due to SO rules (it wont let me)
Blade itself does not have that functionality.
In Laravel 3 there was the Str class - which you could do:
{{ Str::limit($myVariable, 10) }}
At this stage I do not believe the Str class is in Laravel 4 - but here is a port of it that you can include in composer to add to your own project