Download the PHP package neoan3-apps/template without Composer
On this page you can find all versions of the php package neoan3-apps/template. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download neoan3-apps/template
More information about neoan3-apps/template
Files in neoan3-apps/template
Package template
Short Description PHP DOMDocument-based minimal template engine
License MIT
Rated 5.00 based on 2 reviews
Informations about the package template
neoan3-apps/template
PHP template engine
As of version 2, we dropped PHP 7.4 support. You require at least PHP 8.0 or use v1.2.0 of this package.
Installation / Quick start
composer require neoan3-apps/template
Contents
- Templating
- Iterations
- Conditions
- Custom Functions
- Custom Delimiter
- Custom Attributes
- OOP
- Tips
Templating
neoan3-template is not a full blown template engine, but rather what a template engine should be: With modern JavaScript solutions creating a dynamic approach, neoan3-template focuses on the necessities of static rendering.
profile.html
profile.php
output
Main templating methods
embrace($string, $substitutionArray)
Replaces array-keys indicated by double curly braces with the appropriate value
embraceFromFile($fileLocation, $substitutionArray)
Reads content of a file and executes the embrace function.
Iterations (n-for)
The n-for loop evaluates as PHP's foreach and uses the same syntax (excluding the $-sign). In order to access keys, use the PHP markup (items as key => value) to emulate $items as $key => $value. Without the necessity to access keys, use the simple markup items => item.
Output:
Conditions (n-if)
You can attach the n-if attribute to any tag in order to render conditionally.
Curly braces are not required and nesting follows the same rules as general rendering
(meaning that you will write outer.inner
to access $parameters['outer']['inner']
)
n-if is type conscious and therefore evaluates strict.
true | false |
---|---|
false == 'false' | false === 'false' |
Conditions work in nested context and inherit naming.
Example within n-for:
Output:
Custom functions
Unlike other template engines, neoan3-apps/template does not come with expensive additional functionality. It is our believe that most of what other template engines offer should not be included in a template engine. However, you can pass in custom closures to achieve custom transformation or similar.
Example:
Output:
You can even use substitutions like this (as used in php-i18n-translate):
Output:
Custom delimiter
There is a reason why curly braces are used: You can leverage the fact that some values are only potentially filled by the backend and addressed in the front-end if the value does not exist in your data (yet). However, there are also cases where you want to specifically avoid having your front-end framework picking up unfilled variables, or you have special parsing needs to work with various files. Therefore, you can use custom identifiers by providing your desired markup to embrace or embraceFromFile.
Example:
Output:
NOTE: If your delimiter is a tag, the engine will NOT remove the delimiter:
Output:
Custom Attributes
Under the hood, n-if and n-for are just custom attributes. You can add your own attributes and extend the engine to your needs using any callable:
OOP
So far, we have used a purely static approach. However, the "Template" methods are merely facades resulting in the initialization of the Interpreter. If you need more control over what is happening, or it better fits your situation, you are welcome to use it directly:
Tips
There are a few things that are logical, but not obvious:
Parents on loops
Due to processing in hierarchy, many internal parsing operations can cause race-conditions. Imagine the following HTML:
In this scenario, the parser would first hit the attribute n-for
and add p-tags to the parent node (here div). n-for
now takes control of this parent and
interprets the children. As the context gets reevaluated in every loop, but the second p-tag is not iterated,
the resulting output would be:
It is therefore recommended to use one distinct parent when using attribute methods:
Flat array properties
The interpreter "flattens" the given context-array in order to allow for the dot-notation. In this process generic values are added:
output:
If needed, you can use this in logic:
All versions of template with dependencies
ext-mbstring Version *
ext-json Version *
ext-dom Version *
ext-libxml Version *