Releases: Aleksandr-ru/TemplateObject
Forced filter
Forced filter mechanism is now applied instead of default filter for variables.
Forced filter is filter applied to a variable first, if no "raw" filter is set and no same filter presents.
By default forced filter is set to "html" and could be changed via new methods (getter and setter).
Forced filter can contain several elements like html|nl2br
, each will be prepended to variable's filters, if is not set.
Example:
Before 2.7 {{VAR}}
means {{VAR|html}}
and {{VAR|js}}
means only {{VAR|js}}
(no html applied).
Now {{VAR}}
means {{VAR|html}}
and {{VAR|js}}
means {{VAR|html|js}}
(html is forcefully prepended).
To get back old behaviour of {{VAR|js}}
you need to add "raw" filter like {{VAR|raw|js}}
.
New mechanism may cause some backwards compatibility issues, but in most cases everything works as usual.
Composer support
TemplateObject now available via composer!
composer require aleksandr.ru/template-object
🎉️
More efficiency
This release removes old side effect of preserving blocks by setting it to non-assoc array with setVarArray
function.
Example code, see comments for details:
$tpl->setVarArray([
'multiblock1' => [ // ok, will be set twice with given variables from array
['VAR1' => 'val1', 'VAR2' => 'val2'],
['VAR1' => 'val3', 'VAR2' => 'val4'],
],
'multiblock2' => [true], // won't be preserved as empty block
'multiblock3' => [0, 1, 2], // won't be preserved as empty block
'multiblock4' => null, // only this will be preserved as empty block
]);
Template processing boost 🚀
New option public $debug = false
to suppress user-level errors (warnings and notices). Effects when processing large amount of data, see here for performance issue.
Some minor fixes in setVarArray
function logic.
Recursive blocks
Block recursion are here! But be careful using it.
Now you can add special markup to block to make it recursive.
<!-- RECURSION blockname -->
This placeholder will be replaced with entire block content and will be accessible inside the block with given 'blockname'.
So, for example, you can make a recursive lists:
<ul>
<!-- BEGIN listitem -->
<li>
Item of level {{LEVEL}}
<ul>
<!-- RECURSION listitem -->
</ul>
</li>
<!-- END listitem -->
</ul>
The recursion available with direct calls of setBlock
or setVarArray
with nested arrays, for example:
$a = array(
'block' => array(
'LEVEL' => 1,
'block' => array(
'LEVEL' => 2,
'block' => array(
'LEVEL' => 3,
),
),
),
);
$to->setVarArray($a);
Chaining also available like $to->setBlock('block')->setBlock('block')->setBlock('block');
Empty blocks
Now you can preserve empty blocks in setVarArray by setting
'emptyblock' => NULL
The block will be "touched" same as setBlock('emptyblock');
Block options
Now you can add option "rsort" to block markup to get this block output in reverse order. Example:
<!-- BEGIN myblock rsort -->
...
<!-- END myblock -->
More options will be available in further releases.
Also some minor bug fixes.
Introducing global variables
Now you can set a global variable to template and it will be automatically set in all newly created children blocks.
All global variables can be overlapped by setting local variables with the same name.
Extend template
Now templates can extend each other by replacing it's blocks with own content
Get variables and blocks
Ability to get variables and blocks from loaded template