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.
Update for Laravel 7.*: Fluent Strings i.e a more fluent, object-oriented interface for working with string values, allowing you to chain multiple string operations together using a more readable syntax compared to traditional string operations.
limit Example :
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
Output
The quick brown fox...
words Example :
$string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
Output
Perfectly balanced, as >>>
Update for Laravel 6.* : You require this package to work all laravel helpers
composer require laravel/helpers
For using helper in controller, don't forget to include/use class as well
use Illuminate\Support\Str;
Laravel 5.8 Update
This is for handling characters from the string :
{!! Str::limit('Lorem ipsum dolor', 10, ' ...') !!}
Output
Lorem ipsu ...
This is for handling words from the string :
{!! Str::words('Lorem ipsum dolor', 2, ' ...') !!}
Output
Lorem ipsum ...
Here is the latest helper documentation for handling string Laravel Helpers