myerscode / templex
A lightweight, regex based template rendering engine
Installs: 19
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^8.0
- myerscode/utilities-files: ^1.0|^2.0
- myerscode/utilities-strings: ^1.0|^2.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
- symfony/var-dumper: ^5.3
This package is auto-updated.
Last update: 2024-11-12 04:05:24 UTC
README
A lightweight, regex based template rendering engine for PHP
Why this package is helpful?
This package will allow you to define stubs and then hydrate the template using PHP variables.
As the engine uses RegEx it does not rely on using eval
or including and running the code as PHP.
This means that you are able to simply generate new PHP (or any type of text content filled files for that matter) you need.
Install
You can install this package via composer:
composer require myerscode/templex
Usage
$templateDirectory = __DIR__ . '/Resources/Templates/; $templateExtentions = 'stub'; $templex = new Templex($templateDirectory, $templateExtentions); echo $templex->render('index');
Template Directory
Templates
Templates can be any form of text based files. By default, Templex will look for files with .stub
or .template
file extensions.
Templex uses <{
and }>
as opening and closing anchor tags to find and process Slots
, which can be used to generate
dynamic views from placeholders and logic.
Slots
Slots are the "magic" of Templex. They are the isolated functionality that perform replacement and hydrating actions on a template to create the final rendered output.
The default included slots are:
- IncludeSlot - Includes another templates content
- ControlSlot - Process flow based logic, such as foreach, if statements
- VariableSlot - Replaces single placeholders
Includes
To include another template, in order to create reusable stubs you can simply include it by its template name.
<{ include partials.header }>
Conditions
Templex can process nested slots, so having nested control conditions are handled in the order they are found.
Conditions can be variables, numbers, booleans or literal strings and can the usual comparison evaluators.
<{ if( $value === 'foobar' ) }>
...
<{ if( $value > 7 ) }>
...
<{ if( $value == true ) }>
...
<{ if( $value != false ) }>
Examples
<{ if( $value === $condition ) }>
<p>That value was true!</p>
<{ endif }>
<{ if( $value === 'foobar' ) }>
<p>That value was true!</p>
<{ else }>
<p>That value was false!</p>
<{ endif }>
Loops
Loops will take an array variable to create multiple iterations in your template.
Examples
<ul class="row">
<{ foreach( $users as $user ) }>
<li><{ $user }></li>
<{ endforeach }>
</ul>
Variables
Variables are replaced matching anchors are found. They can are passed in at rendering, or created via other slots such as Loops.
Examples
Hi <{ $name }>!
Issues and Contributing
We are very happy to receive pull requests to add functionality or fixes.
Bug reports and feature requests can be submitted on the Github Issue Tracker.
Please read the Myerscode contributing guide for information on our Code of Conduct.
License
The MIT License (MIT). Please see License File for more information.