Download the PHP package tpawl/lite without Composer
On this page you can find all versions of the php package tpawl/lite. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package lite
Short Description Lightweight Template Engine
License MIT
Homepage https://github.com/tpawl/LiTE
Informations about the package lite
LiTE
Lightweight Template Engine
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.
Requirements
LiTE requires PHP 7.3+
Installation
Via Composer
Usage
LiTE for Developers
Basics
The heart of LiTE is a object called the template expression (of class TPawl\LiTE\Expressions\TemplateExpression
).
A template expression object is created like this:
This will create a template expression that looks up the view helpers in the /path/to/view_helpers/
folder.
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.
Outputting the template
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.
Example:
$arguments
is an indexed array of arguments that can be given to the view helper.
Output in the view helper can be done with echo
, print
, printf
, ...
Sub template expression
Objects of the class TPawl\LiTE\Expressions\SubTemplateExpression
are intended for use inside a view helper.
The constructor takes two arguments:
- The first argument is the template as a string.
- The second argument is an associative array of template variables, with the name of the variable as the key and its value.
Example:
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:
LiTE for Template Designers
Template variables
This defines a template variable with name foo
.
The complete expression above is replaced by the value of the template variable foo
.
View helpers
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: ``
The complete expression above is replaced by the output of the view helper BarViewHelper
.
Predefined view helpers
There is the view helper _xmlViewHelper
predefined.
It is called like this:
This will convert to <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
.
It is useful if you want to output XML.
Example
Save the following view helper to a file named MsgViewHelper.php
in any folder:
Suppose you have the following script in the same folder as the above view helper:
If you call the above script in the browser, one possible output could be: