• Stars
    star
    487
  • Rank 90,352 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use filters easily in your blade templates.

Blade Filters

Use string filters easily in Laravel Blade.

If you have any question how the package works, we suggest to read this post: Laravel Blade Filters.

Getting started

You can install the package with composer, running the composer require conedevelopment/blade-filters command.

Using the filters

You can use the filters in any of your blade templates.

Regular usage:

{{ 'john' | ucfirst }} // John

Chained usage:

{{ 'john' | ucfirst | substr:0,1 }} // J

{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31

Passing non-static values:

{{ $name | ucfirst | substr:0,1 }}

{{ $user['name'] | ucfirst | substr:0,1 }}

{{ $currentUser->name | ucfirst | substr:0,1 }}

{{ getName() | ucfirst | substr:0,1 }}

Passing variables as filter parameters:

$currency = 'HUF'

{{ '12.75' | currency:$currency }} // HUF 12.75

Built-in Laravel functionality:

{{ 'This is a title' | slug }} // this-is-a-title

{{ 'This is a title' | title }} // This Is A Title

{{ 'foo_bar' | studly }} // FooBar

Limitations

Echos

Laravel supports three types of echos. Raw – {!! !!}, regular – {{ }} and escaped (legacy) – {{{ }}}. Filters can be used only with regular echos. Also, filters cannot be used in blade directives directly.

Why? Raw should be as it is. Forced escaping should be escaped only, without modification.

Bitwise operators

Bitwise operators are allowed, but they must be wrapped in parentheses, since they are using the same "pipe operator".

{{ ('a''b') | upper }} // C

The Filters

About the filters

Filters are string functions, that are defined in the Pine\BladeFilters\BladeFilters facade. It has several reasons, that are discussed in the Create custom filters section.

The available filters

The package comes with a few built-in filters, also the default Laravel string methods can be used.

Currency

{{ '17.99' | currency:'CHF' }} // CHF 17.99

{{ '17.99' | currency:'€',false }} // 17.99 €

Passing false as the second parameter will align the symbol to the right.

Date

{{ '1999/12/31' | date }} // 1999-12-31

{{ '1999/12/31' | date:'F j, Y' }} // December 31, 1999

Lcfirst

{{ 'Árpamaláta' | lcfirst }} // árpamaláta

Unlike PHP's default lcfirst(), this filter works with multi-byte strings as well.

Reverse

{{ 'ABCDEF' | reverse }} //FEDCBA

Substr

{{ 'My name is' | substr:0,2 }} // My

{{ 'My name is' | substr:3 }} // name is

Trim

{{ '   trim me    ' | trim }} // trim me

Ucfirst

{{ 'árpamaláta' | ucfirst }} // Árpamaláta

Unlike PHP's default ucfirst(), this filter works with multi-byte strings as well.

Supported built-in Str functions

Create custom filters

As it was mentioned before, every filter is a method that can be called through the Pine\BladeFilters\BladeFilters facade. It has several reasons why is this approach better, but let's take the most important ones:

  • It's easy to define custom filters by extending the facade with the BladeFilters::macro(),
  • No extra files, autoloading or class mapping, it's enough to use any service provider to define filters,
  • By default Laravel provides a bunch of handy methods that we can use as filters.

Parameter ordering

PHP is not very strict regarding to function's parameter ordering and this way it's easier to coordiante or override them. Also, sometimes it happens with Laravel's string functions. It's important that only those functions can be used, that accept the parameters in the following order:

  1. The value to be transformed
  2. Any other parameter if needed

For example:

BladeFilters::macro('filterName', function ($value, $param1 = 'default', $param2 = null) {
    return ...;
});

{{ 'string' | filterName:1,2 }}

Defining custom filters

Since the filters are only methods that are defined in the Str facade and the BladeFilters class, to create filters, you need to create a macro in a service provider's boot() method.

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        BladeFilters::macro('substr', function ($value, $start, $length = null) {
            return mb_substr($value, $start, $length);
        });
    }
}

Contribute

If you found a bug or you have an idea connecting the package, feel free to open an issue.

More Repositories

1

bazar

Bazar is an e-commerce package for Laravel applications.
PHP
421
star
2

sprucecss

An open-source, lightweight and modern CSS framework, design system built on Sass. Give your project a solid foundation.
SCSS
212
star
3

i18n

Push your Laravel translations to the front-end and use them easily with JavaScript.
PHP
123
star
4

canvi

A simple vanilla JavaScript off-canvas menu.
JavaScript
108
star
5

policy

Using Laravel's authorization on the front-end.
PHP
97
star
6

simplepay-gateway

OTP SimplePay bővítmény WooCommerce (WordPress) áruházak részére.
PHP
57
star
7

sprucecss-eleventy-documentation-template

A simple documentation template made with Eleventy and Spruce CSS.
SCSS
50
star
8

bite-sized-a11y

Quick and Small Accessibility - Learn something new about accessibility with short and solid articles (less than 150 words).
CSS
28
star
9

root

Root is an admin package for Laravel applications.
PHP
20
star
10

wp-theme-alpha

Alpha is a WordPress blog and magazine theme with clean design, nice code, and Gutenberg support.
CSS
18
star
11

sprucecss-root-admin-template

A static HTML/CSS/JS administration tempalte built on Spruce CSS.
HTML
8
star
12

accessibility-path

Accessibility Path is a collection of resources that can help you to learn web accessibility and inclusive design.
7
star
13

sprucecss-starter-kit

The “Spruce CSS Starter Kit” repository contains a simple foundation for starting your next project with ease using Spruce CSS.
SCSS
6
star
14

billingo-php

Billingo PHP SDK.
PHP
5
star
15

sprucecss-site

The documentation of Spruce CSS.
MDX
5
star
16

bazar-docs

The Bazar Laravel e-commerce package documentation.
4
star
17

wordpress-plugin-template

WordPress plugin skeleton template.
PHP
4
star
18

qkie

Simple cookie management.
JavaScript
3
star
19

laravel-package-template

Laravel package skeleton template.
PHP
3
star
20

sprucecss-eleventy-starter

A minimalistic Eleventy boilerplate setup with Spruce CSS to help you start with your next project.
CSS
3
star
21

cone-site

The new 11ty-based portfolio website of Cone.
CSS
3
star
22

sprucecss-site-eleventy

The documentation of Spruce CSS.
CSS
3
star
23

sprucecss-examples

Simple examples for how to use and how to get started with Spruce CSS.
CSS
2
star
24

bazar-stripe

The Stripe Payment gateway for Bazar.
PHP
2
star