Laravel Blade Filters Package
Published on by Paul Redmond
Blade Filters is a package by Gergo D. Nagy that adds the concept of filters to your blade templates. If you’re familiar with Django’s templating system or Symfony’s Twig templating language, the concept of a filter looks like this:
{{ 'john' | ucfirst }} // John
Alternatively, you could write the same thing like this in Blade without the concept of filters:
{{ ucfirst('john') }}
Chained usage is where I personally feel filters begin to shine over function calls:
{{ 'john' | ucfirst | substr:0,1 }} // J {{ substr(ucfirst('john'), 0, 1) }}
More usefully, here’s how you’d use filters with a template variable:
{{ $currentUser->name | ucfirst | substr:0,1 }}
Finally, this package provides filters that leverage built-in Laravel APIs:
{{ 'This is a title' | slug }} // this-is-a-title {{ 'This is a title' | title }} // This Is A Title {{ 'foo_bar' | studly }} // FooBar
To learn more about this package—including installation instructions—check out the source code on GitHub at thepinecode/blade-filters. Also, the package author Gergo wrote a detailed article that explains how to build a custom ViewServiceProvider
that you should check out!