...
````
### Set
Creates/Updates variable
````html
[% set variable = 'test' %]
[[ variable ]]
[% set variable = 'test2' %]
[[ variable ]]
````
### Extends and Position
Use this tags to define the parent template and load data from the current template into it using the Position tag.
Usage:
Main template
````html
[% position name="title" %]
Home page
[% endposition %]
[% position name="content" %]
Hello, [[ name ]]
[% endposition %]
[% extends src="base.puff.html" %]
````
Parent template
````html
[% position for="title" %]
[% position for="content' %]
````
## Filters system
You can modify some variables before displaying them or if some statement supports filters, you can modify variable before using it in it.
To specify filters for variable, you should specify them by **~** sign.
Example:
````html
[[ variable ~ uppercase ~ transliterate ]]
````
```` html
[[ int_variable ~ round(1) ]]
````
You also can use filters in **for** statement
````html
[% for products ~ uppercase in item %]
[[ item.name ]]
[% end %]
````
You can create your own filters. Read how to do it in **Extensions system** block
## Extensions system
Puff is extensible, so you can create your own modules, which can contain your own statements and filters.
To create module, just create class which implements **Puff\Modules\ModuleInterface** and insert in into **modules** array of **Engine** configuration
````php
$engine = new \Puff\Engine([
'modules' => [
new \Puff\Modules\Core\CoreModule()
new \Puff\Modules\NewModule\MyModule()
]
]);
````
**Important!** Don't forget to initialze CoreModule here if you need all basic statements, such as **for**, **if-else**, etc.
### Making new statement (element)
To make new element, you should create class, which should extend **Puff\Compilation\Element\AbstractElement**
You should specify element's class in your **Module** class's setUp() method:
````php
...
/**
* Returns an array of elements and filters which will be initialized
*
* @return array
*/
public function setUp(): array
{
return [
'elements' => [
'new_element' => new NewElement(),
'another_new_element' => new AnotherNewElement()
],
...
];
}
...
````
Now, your element is accessible in template by **test_element** keyword
Element's **process** method should return PHP code.
````php
";
}
}
````
You can provide some attributes handling rules. By default, it is handling like this:
````
[% element attribute='some_attribute' %]
Process method will get an array:
[
'attribute' => 'some_attribute'
]
````
But if you want to make statement like **for** (which don't use attributes like "attribute='attr'") you can provide your own attributes handling rules by specifying **handleAttributes** method in your element class
It is getting an array of all elements from tokenizer and **should return an array**
For example:
````
[% new_element attribute anotherAttribute ~ 123 %]
handleAttributes() will get an array:
[
'new_element',
'attribute',
'anotherAttribute',
'~',
'123'
]
````
````php
";
}
public function handleAttributes(array $tokenAttributes)
{
if(array_search('attribute', $tokenAttributes)) {
return ['result' => 'attribute found'];
} else {
return ['result' => 'attribute not found'];
}
}
}
````
````
Testing for example above:
[% new attribute %]
Will display: attribute found
[% new %]
Will display: attribute not found
````
### Making new filter
To make new element, you should create class, which should extend **Puff\Compilation\Element\AbstractElement**
You should specify element's **class name** in **Module's** class setUp() method:
````php
/**
* Returns an array of elements and filters which will be initialized
*
* @return array
*/
public function setUp(): array
{
return [
...
'filters' => [
'new_filter' => NewFilter::class
]
];
}
...
````
Your filter class should implement **Puff\Compilation\Filter\FilterInterface**
For example, discover **UpperCaseFilter** code to understand how it works
````php
[
new \Puff\Modules\Core\CoreModule(),
],
'syntax' => new MySyntax()
]);
````
Now, all tags should use new syntax, let's see how we should update template
````html
(@ if variable == 1 @)
Syntax updated!
(@ end @)
````
### Escaping tags
To escape tag, you should set escaping symbols before some tag to tell compiler to ignore it.
Default escaping symbols in Puff is ``//``, but you can edit it by setting your own Syntax class
````html
[% set variable = 1 %]
[[ variable ]]
//[[variable]]
````
Will display:
````
1
[[ variable ]]
````