|
1 | 1 | # Blade Filters
|
2 | 2 |
|
3 |
| -Use string filters easily in Laravel Blade. |
| 3 | +Forked from [`conedevelopment/blade-filters`](https://github.com/conedevelopment/blade-filters), comparing with the original, your can resolve filters with custom resolver. |
4 | 4 |
|
5 |
| -If you have any question how the package works, we suggest to read this post: |
6 |
| -[Laravel Blade Filters](https://pineco.de/laravel-blade-filters/). |
7 | 5 |
|
8 |
| -## Getting started |
9 |
| - |
10 |
| -You can install the package with composer, running the `composer require conedevelopment/blade-filters` command. |
11 |
| - |
12 |
| -## Using the filters |
13 |
| - |
14 |
| -You can use the filters in any of your blade templates. |
15 |
| - |
16 |
| -#### Regular usage: |
17 |
| - |
18 |
| -```php |
19 |
| -{{ 'john' | ucfirst }} // John |
20 |
| -``` |
21 |
| - |
22 |
| -#### Chained usage: |
23 |
| - |
24 |
| -```php |
25 |
| -{{ 'john' | ucfirst | substr:0,1 }} // J |
26 |
| - |
27 |
| -{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31 |
28 |
| -``` |
29 |
| - |
30 |
| -#### Passing non-static values: |
31 |
| - |
32 |
| -```php |
33 |
| -{{ $name | ucfirst | substr:0,1 }} |
34 |
| - |
35 |
| -{{ $user['name'] | ucfirst | substr:0,1 }} |
36 |
| - |
37 |
| -{{ $currentUser->name | ucfirst | substr:0,1 }} |
38 |
| - |
39 |
| -{{ getName() | ucfirst | substr:0,1 }} |
40 |
| -``` |
41 |
| - |
42 |
| -#### Passing variables as filter parameters: |
43 |
| - |
44 |
| -```php |
45 |
| -$currency = 'HUF' |
46 |
| - |
47 |
| -{{ '12.75' | currency:$currency }} // HUF 12.75 |
48 |
| -``` |
49 |
| - |
50 |
| -#### Built-in Laravel functionality: |
51 |
| - |
52 |
| -```php |
53 |
| -{{ 'This is a title' | slug }} // this-is-a-title |
54 |
| - |
55 |
| -{{ 'This is a title' | title }} // This Is A Title |
56 |
| - |
57 |
| -{{ 'foo_bar' | studly }} // FooBar |
58 |
| -``` |
59 |
| - |
60 |
| -### Limitations |
61 |
| - |
62 |
| -#### Echos |
63 |
| - |
64 |
| -Laravel supports three types of echos. Raw – `{!! !!}`, regular – `{{ }}` and escaped (legacy) – `{{{ }}}`. |
65 |
| -Filters can be used **only with regular** echos. Also, filters **cannot be used in blade directives directly**. |
66 |
| - |
67 |
| -> Why? Raw should be as it is. Forced escaping should be escaped only, without modification. |
68 |
| -
|
69 |
| -#### Bitwise operators |
70 |
| - |
71 |
| -Bitwise operators are allowed, but they must be wrapped in parentheses, |
72 |
| -since they are using the same "pipe operator". |
73 |
| - |
74 |
| -```php |
75 |
| -{{ ('a' | 'b') | upper }} // C |
76 | 6 | ```
|
77 |
| - |
78 |
| -## The Filters |
79 |
| - |
80 |
| -### About the filters |
81 |
| - |
82 |
| -Filters are string functions, that are defined in the `Pine\BladeFilters\BladeFilters` facade. |
83 |
| -It has several reasons, that are discussed in the [Create custom filters](#create-custom-filters) section. |
84 |
| - |
85 |
| -### The available filters |
86 |
| - |
87 |
| -The package comes with a few built-in filters, also the default Laravel string methods can be used. |
88 |
| - |
89 |
| -#### Currency |
90 |
| - |
91 |
| -```php |
92 |
| -{{ '17.99' | currency:'CHF' }} // CHF 17.99 |
93 |
| - |
94 |
| -{{ '17.99' | currency:'€',false }} // 17.99 € |
95 |
| -``` |
96 |
| - |
97 |
| -> Passing `false` as the second parameter will align the symbol to the right. |
98 |
| -
|
99 |
| -#### Date |
100 |
| - |
101 |
| -```php |
102 |
| -{{ '1999/12/31' | date }} // 1999-12-31 |
103 |
| - |
104 |
| -{{ '1999/12/31' | date:'F j, Y' }} // December 31, 1999 |
105 |
| -``` |
106 |
| - |
107 |
| -#### Lcfirst |
108 |
| - |
109 |
| -```php |
110 |
| -{{ 'Árpamaláta' | lcfirst }} // árpamaláta |
111 |
| -``` |
112 |
| - |
113 |
| -> Unlike PHP's default `lcfirst()`, this filter works with multi-byte strings as well. |
114 |
| -
|
115 |
| -#### Reverse |
116 |
| - |
117 |
| -```php |
118 |
| -{{ 'ABCDEF' | reverse }} //FEDCBA |
119 |
| -``` |
120 |
| - |
121 |
| -#### Substr |
122 |
| - |
123 |
| -```php |
124 |
| -{{ 'My name is' | substr:0,2 }} // My |
125 |
| - |
126 |
| -{{ 'My name is' | substr:3 }} // name is |
127 |
| -``` |
128 |
| - |
129 |
| -#### Trim |
130 |
| - |
131 |
| -```php |
132 |
| -{{ ' trim me ' | trim }} // trim me |
133 |
| -``` |
134 |
| - |
135 |
| -#### Ucfirst |
136 |
| - |
137 |
| -```php |
138 |
| -{{ 'árpamaláta' | ucfirst }} // Árpamaláta |
139 |
| -``` |
140 |
| - |
141 |
| -> Unlike PHP's default `ucfirst()`, this filter works with multi-byte strings as well. |
142 |
| -
|
143 |
| -### Supported built-in Str functions |
144 |
| - |
145 |
| -- [Str::after()](https://laravel.com/docs/5.8/helpers#method-str-after) |
146 |
| -- [Str::before()](https://laravel.com/docs/5.8/helpers#method-str-before) |
147 |
| -- [Str::camel()](https://laravel.com/docs/5.8/helpers#method-str-camel) |
148 |
| -- [Str::finish()](https://laravel.com/docs/5.8/helpers#method-str-finish) |
149 |
| -- [Str::kebab()](https://laravel.com/docs/5.8/helpers#method-str-kebab) |
150 |
| -- [Str::limit()](https://laravel.com/docs/5.8/helpers#method-str-limit) |
151 |
| -- [Str::plural()](https://laravel.com/docs/5.8/helpers#method-str-plural) |
152 |
| -- [Str::singular()](https://laravel.com/docs/5.8/helpers#method-str-singular) |
153 |
| -- [Str::slug()](https://laravel.com/docs/5.8/helpers#method-str-slug) |
154 |
| -- [Str::snake()](https://laravel.com/docs/5.8/helpers#method-str-snake) |
155 |
| -- [Str::start()](https://laravel.com/docs/5.8/helpers#method-str-start) |
156 |
| -- [Str::studly()](https://laravel.com/docs/5.8/helpers#method-str-studly) |
157 |
| -- [Str::title()](https://laravel.com/docs/5.8/helpers#method-str-title) |
158 |
| - |
159 |
| -## Create custom filters |
160 |
| - |
161 |
| -As it was mentioned before, every filter is a method that can be called through the `Pine\BladeFilters\BladeFilters` facade. |
162 |
| -It has several reasons why is this approach better, but let's take the most important ones: |
163 |
| - |
164 |
| -- It's easy to define custom filters by extending the facade with the `BladeFilters::macro()`, |
165 |
| -- No extra files, autoloading or class mapping, it's enough to use any service provider to define filters, |
166 |
| -- By default Laravel provides a bunch of handy methods that we can use as filters. |
167 |
| - |
168 |
| -### Parameter ordering |
169 |
| - |
170 |
| -PHP is not very strict regarding to function's parameter ordering and this way it's easier to coordiante or override them. |
171 |
| -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: |
172 |
| - |
173 |
| -1. The value to be transformed |
174 |
| -2. Any other parameter if needed |
175 |
| - |
176 |
| -For example: |
177 |
| - |
178 |
| -```php |
179 |
| -BladeFilters::macro('filterName', function ($value, $param1 = 'default', $param2 = null) { |
180 |
| - return ...; |
181 |
| -}); |
182 |
| - |
183 |
| -{{ 'string' | filterName:1,2 }} |
184 |
| -``` |
185 |
| - |
186 |
| -### Defining custom filters |
187 |
| - |
188 |
| -Since the filters are only methods that are defined in the `Str` facade and the `BladeFilters` class, to create filters, |
189 |
| -you need to create a macro in a service provider's `boot()` method. |
190 |
| - |
191 |
| -```php |
192 |
| -class AppServiceProvider extends ServiceProvider |
193 |
| -{ |
194 |
| - public function boot() |
195 |
| - { |
196 |
| - BladeFilters::macro('substr', function ($value, $start, $length = null) { |
197 |
| - return mb_substr($value, $start, $length); |
198 |
| - }); |
199 |
| - } |
200 |
| -} |
| 7 | + $assetContextResolver = function(){ |
| 8 | + return '(isset($local_asset_context)? $local_asset_context: $base_asset_context)->'; |
| 9 | + }; |
| 10 | + BladeFiltersCompiler::extend('theme_asset_url', $assetContextResolver); |
| 11 | + BladeFiltersCompiler::extend('theme_asset_import', $assetContextResolver); |
201 | 12 | ```
|
202 | 13 |
|
203 |
| -## Contribute |
204 | 14 |
|
205 |
| -If you found a bug or you have an idea connecting the package, feel free to open an issue. |
| 15 | +Please check original repo for other documents. |
0 commit comments