davidphtee / templiphy
A simple PHP template engine with moustache syntax: {{ $variable }} and {{{ PHP code }}}.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/davidphtee/templiphy
Requires
- php: >=7.4
This package is auto-updated.
Last update: 2025-12-17 10:29:19 UTC
README
A lightweight PHP template engine with moustache syntax for easy variable insertion and PHP code execution within your HTML templates.
Features
✅ Moustache Syntax
{{ $variable }}— Outputs a variable’s value{{{ PHP code }}}— Executes raw PHP code
✅ Development & Production Modes
- Development mode uses a temporary file for better error tracing.
- Production mode uses
eval()for faster rendering.
✅ Safe Variable Handling
- Variables are extracted safely.
- Errors are handled gracefully with meaningful messages.
Installation
Simply include Template.php in your project.
require_once 'Template.php';
Usage
1️⃣ Create a Template instance
$template = new Template(true); // `true` enables development mode
2️⃣ Set variables
$template->set('title', 'Hello World'); $template->setArray([ 'name' => 'John', 'age' => 30 ]);
3️⃣ Create a template file
example.tpl
<h1>{{ $title }}</h1> <p>Name: {{ $name }}</p> <p>Age: {{ $age }}</p> {{{ if($age >= 18): }}} <p>Access granted.</p> {{{ else: }}} <p>Access denied.</p> {{{ endif; }}}
4️⃣ Render the template
echo $template->fetch('example.tpl');
Configuration
| Parameter | Description |
|---|---|
isDevelopment |
true (default): uses a temporary file for detailed error tracing. false: uses eval() for faster rendering in production. |
Class Reference
__construct(bool $isDevelopment = true)
Create a new instance.
true→ development modefalse→ production mode
set(string $name, mixed $value)
Set a single variable.
setArray(array $list)
Set multiple variables at once.
fetch(string $file): string
Render the given template file with the current variables.
Error Handling
- Throws
RuntimeExceptionif:- The template file does not exist or is unreadable.
- An error occurs while rendering the template.
- In development mode, the source file name and line numbers are preserved for easier debugging.
License
MIT — use it freely!
Author
David Tee