LiTE
Lightweight Template Engine, (*1)
, (*2)
Description
LiTE is an ad hoc Template Engine especially suited for Backends.
It is native, because it uses PHP as its Template Language and needs no compiling of templates.
It supports Template Variables and View Helpers., (*3)
Requirements
LiTE requires PHP 7.3+, (*4)
Installation
Via Composer, (*5)
$ composer require tpawl/lite
Usage
LiTE for Developers
Basics
The heart of LiTE is a object called the template expression (of class TPawl\LiTE\Expressions\TemplateExpression
)., (*6)
A template expression object is created like this:, (*7)
$settings = [
$template, // a string holding the template
['the' => 'variables', 'go' => 'here'],
'/path/to/view_helpers',
'view_helpers\namespace',
];
$templateExpression = new TPawl\LiTE\Expressions\TemplateExpression($settings);
This will create a template expression that looks up the view helpers in the /path/to/view_helpers/
folder., (*8)
Note that the only argument of the template expression is an array of settings options.
* The first option is the template as a string.
* The second option is an associative array of template variables, with the name of the variable as the key and its value.
* The third option is the path to the folder in which the view helpers are stored.
* The fourth option is the namespace in which the view helpers are defined as a string. If you do not use a namespace for your view helpers, write the empty string (''
) here., (*9)
Outputting the template
$templateExpression->display();
Defining a view helper
A view helper is a class that implements the interface TPawl\LiTE\ViewHelperInterface
, that has a static execute
method.
The name of that class must be ending with ViewHelper
and the first letter must be upper-case.
The code for this class must be saved in a file with the name of the corresponding class with a trailing .php
.
This file must be stored in a folder given as the third configuration option of the template expression., (*10)
Example:, (*11)
'variables', 'go' => 'here']);
$subTemplateExpression->display();
} else {
$subTemplateExpression = new SubTemplateExpression(
$templateB, ['the' => 'variables', 'go' => 'here']);
$subTemplateExpression->display();
}
}
}
```
#### Miscellaneous
To determine wether you are inside/outside of a view helper you can use the static method `TPawl\LiTE\Context\Context::isEmpty()`.
It returns `false` if you are inside a view helper, `true` otherwise.
There is the class `TPawl\LiTE\Version` defined, that holds version information for **LiTE**.
It has the following constants defined:
* `MAJOR` for the major version number (major release).
* `MINOR` for the minor version number (minor release).
* `REVISION` for the revision number (patch level).
Examples:
```php
use TPawl\LiTE\PackageInformations;
echo 'Powered by ', PackageInformations::PACKAGE_NAME , ' ', PackageInformations::makePackageVersionString();
echo 'Copyright © ', PackageInformations::PACKAGE_COPYRIGHT['years'], ' by ', PackageInformations::makePackageCopyrightHoldersString();
```
### LiTE for Template Designers
#### Template variables
```
foo; ?>
This defines a template variable with name foo
.
The complete expression above is replaced by the value of the template variable foo
., (*12)
View helpers
<?php self::bar(); ?>
This calls the view helper BarViewHelper
with no arguments.
If you have arguments, you have to write them as a comma separated list between the parentheses after bar
.
Example: <?php self::bar('arg1', 'arg2', ...); ?>
, (*13)
The complete expression above is replaced by the output of the view helper BarViewHelper
., (*14)
Predefined view helpers
There is the view helper _xmlViewHelper
predefined.
It is called like this:, (*15)
<?php self::_xml('version="1.0" encoding="UTF-8" standalone="yes"'); ?>
This will convert to <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
.
It is useful if you want to output XML., (*16)
Example
Save the following view helper to a file named MsgViewHelper.php
in any folder:, (*17)
= 0) {
$msg = 'Everything is fine';
} else {
$msg = 'This is not good';
}
print $msg;
}
}
```
Suppose you have the following script in the same folder as the above view helper:
```php
Hello name; ?>!<br>
You are running PHP ver; ?>:
HTML;
$variables = [
'name' => 'Thomas',
'ver' => PHP_VERSION,
];
$settings = [
$template,
$variables,
'.',
'',
];
$templateExpression = new TPawl\LiTE\Expressions\TemplateExpression($settings);
$templateExpression->display();
If you call the above script in the browser, one possible output could be:, (*18)
Hello Thomas!
You are running PHP 7.1.10: Everything is fine