-
Notifications
You must be signed in to change notification settings - Fork 121
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;
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
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 -->
https://github.com/EFTEC/BladeOne/blob/master/examples/test_pipe.php
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.
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
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 |
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)
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 }}
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);
Copyright Jorge Castro Castillo
- BladeOne Manual
- Template tags (views)
- Custom control
- Methods of the class
- Injecting logic before the view (composer)
- Extending the class
- Using BladeOne with YAF Yet Another Framework
- Differences between Blade and BladeOne
- Comparision with Twig (May-2020)
- Changelog
- Changes between 2.x and 3.0 and TODO
- Code Protection (Sourceguardian and similars)