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.
Globals
These are automatically accessible inside templates:
Name |
Description |
|---|---|
app_name |
Application name (from config or fallback) |
base_url |
Base URL of the application |
site_url |
Site URL with index.php if not removed |
Example:
<title>{{ app_name }}</title>
<a href="{{ site_url }}/dashboard">Dashboard</a>
Filters
Filters are applied using the pipe | operator in templates:
Filter Name |
Description |
|---|---|
upper |
Converts string to uppercase |
lower |
Converts string to lowercase |
title |
Converts string to title case |
capitalize |
Capitalizes first letter |
trim |
Removes whitespace |
reverse |
Reverses string or array |
escape / e |
Escapes HTML characters |
raw |
Outputs value without escaping |
nl2br |
Converts newlines to <br> |
length |
Returns length of string or array |
default |
Returns default if value is empty |
join |
Joins array elements |
slice |
Extracts substring or sub-array |
number_format |
Formats number |
abs |
Returns absolute value |
round |
Rounds a number |
date |
Formats date |
json_encode |
Converts value to JSON |
replace |
Performs key-value replacement |
money |
Formats number as currency (₱ by default) |
slug |
Converts string to URL slug |
pluralize |
Adds “s” if count not 1 |
Example:
<p>{{ username | upper }}</p>
<p>{{ message | escape }}</p>
<p>{{ 123456.789 | money }}</p>
<p>{{ 5 | pluralize("apple") }}</p>
Functions
Functions can be called in templates:
Function Name |
Description |
|---|---|
url(path) |
Returns a clean URL path |
asset(path) |
Returns a public asset URL |
site_url(path) |
Returns a site-relative URL |
base_url(path) |
Returns base URL + path |
active(route) |
Returns “active” if current route matches |
config(key) |
Returns a config value |
session(key) |
Returns session variable or all if key is null |
upper(str) |
Converts string to uppercase |
repeat(str, n) |
Repeats string n times |
now() |
Returns current date and time |
date(format, time) |
Returns formatted date |
include(template, vars) |
Includes another template |
dump(value) |
Debug helper: prints value with <pre> |
csrf_field() |
Prints CSRF hidden input field |
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.