A lightweight and efficient PHP template engine that supports variables, nested conditions, and basic template operations. Compatible with PHP 5.6 and above.
This software is developed during my free time and I will be glad if somebody will support me.
Everyone's time should be valuable, so please consider donating.
https://buymeacoffee.com/oxcakmak
- Variable replacement with dot notation support
- Conditional statements (if, elseif, else)
- Nested conditions
- HTML escaping
- Array and object access
- Boolean and numeric comparisons
- Performance optimized
Simply include the Template.php file in your project:
require_once 'Template.php';
// Initialize template engine with template directory and extension
$template = new Template(__DIR__ . '/views', 'tpl');
// Assign variables
$userData = [
'name' => 'John',
'age' => '26',
'details' => [
'balance' => 100,
'verified' => true
],
'status' => 'active'
];
$template->assign('user', $userData);
// Load and render template
echo $template->load('home');
<h1>Hello {{ user.name }}</h1>
<p>You are {{ user.age }} years old</p>
{% if user.name == "John" && user.age == 26 %}
<h1>Hello John</h1>
<p>You are 26 years old</p>
{% endif %}
{% if user.details.verified === true && user.status === "active" %}
<p>Verified active user</p>
{% elseif user.status === "pending" %}
<p>Pending user</p>
{% else %}
<p>Unverified user</p>
{% endif %}
{% if user.details.balance > 100 %}
<p>Balance is greater than 100</p>
{% elseif user.details.balance === 100 %}
<p>Balance is exactly 100</p>
{% elseif user.details.balance === 5 %}
<p>Balance is 5</p>
{% elseif user.details.balance === 0 %}
<p>Balance is zero</p>
{% endif %}
===
Strict equality==
Loose equality>
Greater than<
Less than>=
Greater than or equal<=
Less than or equal&&
Logical AND||
Logical OR
<ul>
{% for product in products %}
<li>
<h2>{{ product.name }}</h2>
<p>Price: {{ product.price }}</p>
<p>{{ product.description }}</p>
</li>
{% endfor %}
</ul>
<ul>
{% for product in products %}
<li>
<h2>{{ product.name }}</h2>
<p>Price: {{ product.price }}</p>
<p>{{ product.description }}</p>
<p>Colors:</p>
<ul>
{% for color in product.colors %}
<li>{{ color }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
- PHP 5.6 or higher
- File system read permissions for template directory
This project is open-source and available under the MIT License.
Feel free to submit issues and enhancement requests!