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 @extends and @yield

  • Section management using @section and @endsection

  • Template inclusion via @include

  • Custom 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

templates_path

Directory path to template files.

cache_path

Directory path to compiled (cached) templates.

globals

Global variables accessible in all templates.

functions

Custom functions callable within templates.

filters

Registered filters for variable transformation.

auto_escape

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: &lt;b&gt;Hello&lt;/b&gt;

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

$template

string

Template file name (without extension)

$context

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('layout')

Extends a base layout template

@extends('layouts.main')

@section('name') / @endsection

Defines a content section

@section('title') Home @endsection

@yield('name')

Displays section content

@yield('content')

@include('file')

Includes another template

@include('partials.header')

@if / @elseif / @else / @endif

Conditional logic

@if($user) Welcome @endif

@foreach / @endforeach

Loop structure

@foreach($users as $user) {{ user.name }} @endforeach

@php / @endphp

Raw PHP code

@php echo date('Y'); @endphp

{{ var }}

Escaped variable output

{{ username }}

{!! var !!}

Raw (unescaped) output

{!! html_content !!}

{{ var|filter }}

Applies a registered filter

{{ name|upper }}

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 public/ directory

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.php and 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.