Ember Template Engine
Overview
The Ember class is a lightweight templating engine for the LavaLust PHP Framework. It allows developers to use a simplified syntax for templates, supporting sections, includes, template inheritance, custom filters, and functions.
Ember combines simplicity and flexibility, compiling templates into PHP for fast execution.
Features
Template inheritance using
@extendsand@yieldSection management using
@sectionand@endsectionTemplate inclusion via
@includeCustom filters and functions
Escaped and raw output syntax
Auto-escaping support for XSS prevention
Built-in control structures:
@if,@foreach,@while, etc.
Class Summary
Property / Method |
Description |
|---|---|
|
Directory path to template files. |
|
Directory path to compiled (cached) templates. |
|
Global variables accessible in all templates. |
|
Custom functions callable within templates. |
|
Registered filters for variable transformation. |
|
Boolean that controls whether HTML escaping is automatically applied. |
Configuration Example (app/config/ember.php)
$config['ember_helper_enabled'] = TRUE;
$config['cache_path'] = ROOT_DIR . 'runtime/cache/';
$config['templates_path'] = APP_DIR . 'views/';
$config['auto_escape'] = TRUE;
Public Methods
escape()
Syntax:
$this->ember->escape($value);
Description:
Escapes a value using htmlspecialchars() if auto_escape is enabled.
Example:
echo $this->ember->escape('<b>Hello</b>');
// Output: <b>Hello</b>
add_global()
Syntax:
$this->ember->add_global($name, $value);
Description: Registers a global variable available to all templates.
Example:
$this->ember->add_global('appName', 'MyLavaApp');
Usage in Template:
<p>Welcome to {{ appName }}</p>
add_function()
Syntax:
$this->ember->add_function($name, $callable);
Description: Registers a callable function accessible from within templates.
Example:
$this->ember->add_function('site_url', fn($path = '') => site_url($path));
Usage in Template:
<a href="{{ site_url('dashboard') }}">Dashboard</a>
add_filter()
Syntax:
$this->ember->add_filter($name, $callable);
Description: Registers a custom filter for transforming variable output.
Example:
$this->ember->add_filter('upper', fn($v) => strtoupper($v));
$this->ember->add_filter('lower', fn($v) => strtolower($v));
Usage in Template:
<p>{{ username|upper }}</p>
apply_filter()
Syntax:
$this->ember->apply_filter($name, $value);
Description: Applies a registered filter to a given value.
Example:
echo $this->ember->apply_filter('upper', 'ronald');
// Output: RONALD
render()
Syntax:
$this->ember->render($template, $context = []);
Description: Renders a template with the provided context data.
Parameters:
Name |
Type |
Description |
|---|---|---|
|
string |
Template file name (without extension) |
|
array |
Variables passed to the template |
Example:
echo $this->ember->render('pages.profile', ['name' => 'Ron']);
Template (``views/pages/profile.ember.php``):
<h1>Hello, {{ name|upper }}</h1>
Output:
Hello, RON
Template Directives
Directive |
Purpose |
Example |
|---|---|---|
|
Extends a base layout template |
|
|
Defines a content section |
|
|
Displays section content |
|
|
Includes another template |
|
|
Conditional logic |
|
|
Loop structure |
|
|
Raw PHP code |
|
|
Escaped variable output |
|
|
Raw (unescaped) output |
|
|
Applies a registered filter |
|
Example Template Structure
File: views/layouts/main.ember.php
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@include('partials.header')
<main>
@yield('content')
</main>
</body>
</html>
File: views/partials/header.ember.php
<header><h1>My Application</h1></header>
File: views/pages/home.ember.php
@extends('layouts.main')
@section('title')
Welcome Page
@endsection
@section('content')
<h1>Hello, {{ name|upper }}</h1>
@endsection
Output:
Hello, RON
Ember Registrar
- class Ember_registrar.Ember_registrar
The Ember_registrar class provides a collection of default filters, globals, and functions that extend the Ember template engine in the LavaLust Framework.
It acts as a registration hub where you can define custom filters, functions, and global variables that become available to all templates rendered through the Ember engine.
This class is automatically called inside the Ember class upon initialization.
Name |
Description |
|---|---|
app_name |
Application name from config (default: “LavaLust App”) |
base_url |
Base URL of the application |
site_url |
Site URL with index.php (if applicable) |
csp_nonce |
Content Security Policy nonce for inline scripts/styles |
Filter |
Description |
|---|---|
upper |
Converts string to uppercase |
lower |
Converts string to lowercase |
title |
Converts string to Title Case |
capitalize |
Capitalizes first letter |
trim |
Removes whitespace from both ends |
reverse |
Reverses string or array |
Filter |
Description |
|---|---|
escape / e |
Escapes output (recommended for safety) |
raw |
Outputs raw HTML (dangerous, use carefully) |
nl2br |
Converts newlines to <br> |
striptags |
Removes HTML tags |
Filter |
Description |
|---|---|
length |
Counts items in array or characters in string |
default(value) |
Returns default if value is empty |
join(separator) |
Joins array into string |
slice(start, length) |
Extracts part of array or string |
Filter |
Description |
|---|---|
number_format(decimals) |
Formats number with decimals |
abs |
Absolute value |
round(precision) |
Rounds number |
Filter |
Description |
|---|---|
date(format) |
Formats date/time value |
Filter |
Description |
|---|---|
json(pretty) |
Converts value to JSON string |
replace(search, replace) |
Replaces text inside string |
Filter |
Description |
|---|---|
money(symbol) |
Formats number as currency (default ₱) |
slug |
Converts string to URL-friendly slug |
pluralize(word) |
Adds “s” if count is not 1 |
Function Name |
Description |
|---|---|
url(path) |
Returns a clean relative URL (prepends “/”) |
asset(path) |
Returns URL from |
site_url(path) |
Returns routed URL |
base_url(path) |
Returns base URL + path |
active(route) |
Returns “active” if route matches current URL |
Function Name |
Description |
|---|---|
config(key) |
Returns config value |
session(key) |
Returns session value; returns all if key is null |
Function Name |
Description |
|---|---|
upper(str) |
Converts string to uppercase |
repeat(str, n) |
Repeats string n times |
Function Name |
Description |
|---|---|
now() |
Returns current datetime (Y-m-d H:i:s) |
date(format, timestamp) |
Formats timestamp |
Function Name |
Description |
|---|---|
include(template, vars) |
Renders another template with variables |
dump(value, die=false) |
Prints formatted debug output; optionally stops execution |
Function Name |
Description |
|---|---|
csrf_field() |
Outputs hidden CSRF input field |
old(key, default) |
Returns previously submitted form value |
Function Name |
Description |
|---|---|
nonce() |
Returns CSP nonce for inline scripts/styles |
Function Name |
Description |
|---|---|
asset_version(path) |
Returns asset URL with cache-busting version (?v=timestamp) |
Example:
<img src="{{ asset('images/logo.png') }}" alt="Logo">
<form method="post">
{{ csrf_field() }}
</form>
<p>{{ now() }}</p>
Extending the Registrar
You can extend Ember_registrar to register additional filters, functions, or globals:
<?php
class EmberConfigs extends Ember_registrar {
public function register($engine)
{
// Add a new global
$engine->add_global('myName', 'Ron');
// Add a new filter
$engine->add_filter('greet', fn($name) => 'Hello, ' . ucfirst($name) . '!');
// Add a new function
$engine->add_function('sum', fn($a, $b) => $a + $b);
}
}
Example in template:
<p>{{ myName | greet }}</p>
<p>2 + 3 = {{ sum(2, 3) }}</p>
Note
Default filters and functions are automatically registered. Custom registrars can override or extend existing definitions. Place extended registrar under
app/libraries/EmberConfigs.phpand load via:
$this->call->library('EmberConfigs');
$this->EmberConfigs->register($this->ember);
Summary
Templates are automatically cached to the configured cache_path.
Filters and functions must be registered before rendering.
Use {!! !!} only for trusted HTML output.
Auto-escaping is controlled globally via the auto_escape configuration option.