Skip to content

Commit

Permalink
Allow to resolve filters with custom resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
vidy committed May 23, 2022
1 parent 4fd2445 commit 9108a26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 200 deletions.
204 changes: 7 additions & 197 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,205 +1,15 @@
# Blade Filters

Use string filters easily in Laravel Blade.
Forked from [`conedevelopment/blade-filters`](https://github.com/conedevelopment/blade-filters), comparing with the original, your can resolve filters with custom resolver.

If you have any question how the package works, we suggest to read this post:
[Laravel Blade Filters](https://pineco.de/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:

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

#### Chained usage:

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

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

#### Passing non-static values:

```php
{{ $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:

```php
$currency = 'HUF'

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

#### Built-in Laravel functionality:

```php
{{ '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".

```php
{{ ('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](#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

```php
{{ '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

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

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

#### Lcfirst

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

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

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

#### Substr

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

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

#### Trim

```php
{{ ' trim me ' | trim }} // trim me
```

#### Ucfirst

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

> Unlike PHP's default `ucfirst()`, this filter works with multi-byte strings as well.
### Supported built-in Str functions

- [Str::after()](https://laravel.com/docs/5.8/helpers#method-str-after)
- [Str::before()](https://laravel.com/docs/5.8/helpers#method-str-before)
- [Str::camel()](https://laravel.com/docs/5.8/helpers#method-str-camel)
- [Str::finish()](https://laravel.com/docs/5.8/helpers#method-str-finish)
- [Str::kebab()](https://laravel.com/docs/5.8/helpers#method-str-kebab)
- [Str::limit()](https://laravel.com/docs/5.8/helpers#method-str-limit)
- [Str::plural()](https://laravel.com/docs/5.8/helpers#method-str-plural)
- [Str::singular()](https://laravel.com/docs/5.8/helpers#method-str-singular)
- [Str::slug()](https://laravel.com/docs/5.8/helpers#method-str-slug)
- [Str::snake()](https://laravel.com/docs/5.8/helpers#method-str-snake)
- [Str::start()](https://laravel.com/docs/5.8/helpers#method-str-start)
- [Str::studly()](https://laravel.com/docs/5.8/helpers#method-str-studly)
- [Str::title()](https://laravel.com/docs/5.8/helpers#method-str-title)

## 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:

```php
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.

```php
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
BladeFilters::macro('substr', function ($value, $start, $length = null) {
return mb_substr($value, $start, $length);
});
}
}
$assetContextResolver = function(){
return '(isset($local_asset_context)? $local_asset_context: $base_asset_context)->';
};
BladeFiltersCompiler::extend('theme_asset_url', $assetContextResolver);
BladeFiltersCompiler::extend('theme_asset_import', $assetContextResolver);
```

## Contribute

If you found a bug or you have an idea connecting the package, feel free to open an issue.
Please check original repo for other documents.
18 changes: 15 additions & 3 deletions src/BladeFiltersCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class BladeFiltersCompiler
{
protected static $containers = [];

/**
* Compile the echo statements.
*
Expand Down Expand Up @@ -35,19 +37,29 @@ protected function parseFilters($value)
return $value;
}


$wrapped = '';

foreach ($filters as $key => $filter) {
$filter = preg_split('/:(?=(?:[^\'\"\`]*([\'\"\`])[^\'\"\`]*\1)*[^\'\"\`]*$)/u', trim($filter));

$filterName = $filter[0];

$containered = isset(self::$containers[$filterName]) ? call_user_func(self::$containers[$filterName], $filterName): BladeFilters::class.'::';

$wrapped = sprintf(
'\Pine\BladeFilters\BladeFilters::%s(%s%s)',
$filter[0],
$containered.'%s(%s%s)',
$filterName,
$key === 0 ? trim(str_replace($matches[0], '', $value)) : $wrapped,
isset($filter[1]) ? ",{$filter[1]}" : ''
);
}

return $wrapped;
}
}

public static function extend($name, callable $container)
{
self::$containers[$name] = $container;
}
}

0 comments on commit 9108a26

Please sign in to comment.