35

Laravel Blade Filters Package

 5 years ago
source link: https://www.tuicool.com/articles/hit/Y7rMfmR
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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!

Filed in: News


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK