Download the PHP package wol-soft/php-micro-template without Composer
On this page you can find all versions of the php package wol-soft/php-micro-template. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wol-soft/php-micro-template
More information about wol-soft/php-micro-template
Files in wol-soft/php-micro-template
Package php-micro-template
Short Description A minimalistic templating engine for PHP
License MIT
Homepage https://github.com/wol-soft/php-micro-template
Informations about the package php-micro-template
php-micro-template
A minimalistic, lightweight templating engine for PHP based on regular expressions.
Features
- Replace variables inside a template
- Iterate over an array or iterable object
- Conditional sections
- Pass objects
- call functions
Requirements
- Requires at least PHP 7.1
Installation
The recommended way to install php-micro-template is through Composer:
Examples
First create your template file:
Afterwards create a new instance of the Render class and render your template:
Instead of saving your templates into files you can also prepare a string which contains the template:
Replacement of variables
Values which are assigned to the template and used directly will be casted to string. For assigned objects you can call methods which return a value. Afterwards the returned value will be casted to string. As constant values integer numbers, strings in single quotes and booleans (true, false) are supported.
Your provided data may be a nested array which can be resolved in the template:
Also, public properties of objects may be accessed from the template:
By default, a used variable which is not provided will result in an UndefinedSymbolException
. You can register a callback function via onResolveError
to handle unresolved variable errors. The callback function must implement the signature function (string $unresolvedVariable): string
. The provided $unresolvedVariable
will contain the whole expression which failed to resolve (eg. myUnresolvedVariable
, myUnresolvedObject.render(var1, var2)
).
Loops
If you assign an array or an iterable object you can use the foreach loop to iterate.
All variables of the parent scope are available inside the loop as well as the current item of the loop. Multiple foreach loops can be nested (compare tests). You can also provide a function which returns an array or an iterable object:
Loops support the usage of key value pairs:
Conditional sections
With the if statement you can create conditional sections. As a condition you can pass either a value which will be casted to bool or call a method on an object. In this case the return value of the function will be casted to bool. Neither multiple values in a single condition combined by operators nor calculations or similar additional functions are provided. For advanced conditions compare the section function calls with a ViewHelper-Object.
Multiple if statements can be nested. To invert an if condition the keyword not can be used:
function calls
The methods which are called can take parameters. Allowed parameters are variables taken out of the current scope or another function call on an object available in the current scope as well as the supported constant values integer numbers, strings in single quotes and booleans (true, false). As an example a ViewHelper-Object can be assigned to the render process and methods of the ViewHelper can be used in the template for advanced logic inside the template.
Additionally, PHP global functions can be used directly in the template as well as assigned callback methods:
Whitespace tolerance
The templating syntax is whitespace tolerant so a template like the one below would be perfectly fine: