Is there a truncate modifier for the blade templates in Laravel, pretty much like Smarty?
I know I could just write out the actual php in the template but i'm looking for something a little nicer to write (let's not get into the whole PHP is a templating engine debate).
So for example i'm looking for something like:
{{ $myVariable|truncate:"10":"..." }}
I know I could use something like Twig via composer but I'm hoping for built in functionality in Laravel itself.
If not is it possible to create your own reusable modifiers like Smarty provides. I like the fact that Blade doesn’t overkill with all the syntax but I think truncate is a real handy function to have.
I'm using Laravel 4.
You can Set the namespace like:
Laravel 6 Update:
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 more Laravel helper functions http://laravel.com/docs/helpers#strings
For using helper in controller, don't forget to include/use class as well
Laravel 5.8 Update
This is for handling characters from the string :
Output
This is for handling words from the string :
Output
Here is the latest helper documentation for handling string Laravel Helpers