Skip to content

Template Pipes (Filter)

Jorge Castro edited this page Sep 18, 2020 · 10 revisions

Template Pipes (Filter)

This library allows adding a filter (conversion or show default values) to the results. By default this functionality is disabled, to enable the pipes (filter), we must set the field "$pipeEnable" to true (by default this value is false)

$blade=new BladeOne();
$blade->pipeEnable=true;

What is a pipe (filter)?

Let's say we have the next value $name='Jack Sparrow'

Our view could look like:

 {{$name}}  or {!! $name !!}

What if we want to show the name in uppercase?.

We could do in our code $name=strtoupper('Jack Sparrow'). With Pipes, we could do the same as follow:

 {{$name | strtoupper}} // strtoupper($name)

We could also add arguments and chain methods.

 {{$name | strtoupper | substr:0,5}} // substr(strtoupper($name),0,5)

Note: This functionality considers the symbol pipes | to separate the methods. It is possible to escape the pipe by using |
Note: The system does not consider quotes or double quotes to find the pipes |, so:

{{ 'value|value2' | strtoupper) }} // Will not work as expected

Default value

Pipes also works to show a default value if the variable used is not defined. It is defined as follow

$variable | default value

Where the default value must be a number, an array, or a literal (starting with " or ')

{!! $name2 | "default"  !!} <!-- if $name2 is undefined, then it shows "default" -->
{!! $name2 | 'default'  !!} <!-- if $name2 is undefined, then it shows "default" -->
{!! $name2 | $othername  !!} <!-- if $name2 is undefined, then it shows the value of $othername -->
{!! $name2 | 555 !!} <!-- if $name2 is undefined, then it shows 555 -->

Examples

https://github.com/EFTEC/BladeOne/blob/master/examples/test_pipe.php

Arguments

We could add extra arguments to each method using the symbol ':', followed by the argument(s).

Syntax without arguments: [method name]
Syntax with arguments: [method name]:[argument2],[argument3],... // The first argument is added automatically.

Stacking

It is also possible to stack methods

Syntax: [value] | [method #1] | [method #2] .... Where Method #1 is executed initially (using value as argument), then Method #2

Using or Adding new Methods

Build-in function.

Function Description Example
format Format a DateTime or a string/number.
If the input is DateTime then it uses DateTime->Format.
Otherwise, it uses sprintf
$date | format:'y/m/d'
$number | format:'%08.4f'
dump It var_dump a variable $var | dump

Using or Adding global functions

We could use all global function (strtoupper(),strtolower(),round(),etc.). It includes user defined functions, if they meet the next requirements:

  • They must be defined in the root namespace or it must set the namespace.
  • The first argument must be the value to pass (or zero arguments). For example, we couldn't use str_replace directly.
  • The function must not exist inside BladeOne, or it's extensions because it has precedence.
  • The function must not exist as a directive, because it has precedence.
function method2($arg=null) {
    return 'it is the method 2 '.$arg;
}

View:

 {{$name | strlen }} // strlen($name)
 {{$name | method2}} // method2($name)

Adding via Directive

We could define a new function as a directive

$methodOne = static function ($arg=null) {
    echo 'It is the method 1 '.$arg;
};
$blade->directive('method1', $methodOne);

View:

 {{$name | method1 }}

Adding via Method

We could use a method defined in our BladeOne, or in its extensions via inheritance or traits. The method must be public.

View:

 {{$name | dump }} // $this->dump($name);
Clone this wiki locally