IDS PHP Libraries

TemplateEngine
in package
uses Loggable

Template Engine for rendering PHP templates with a simple syntax

Features:

  • Template inheritance with {% extends "template" %}
  • Variable output with {{ variable }} (escaped) or {{ variable|raw }} (unescaped)
  • Conditionals with {% if condition %}, {% elseif condition %}, {% else %}, {% endif %}
  • Loops with {% for item in array %} or {% for key, value in array %}
  • Template includes with {% include "template" %}
  • Custom filters with {{ variable|filter }}

Table of Contents

Properties

$cacheDir  : string
$cacheLifetime  : int
$compiledTemplate  : array<string|int, mixed>|bool
$debug  : bool
$filters  : array<string|int, mixed>
$logger  : object|null
$loopVariables  : array<string|int, mixed>
$templateDir  : string

Methods

__construct()  : mixed
Initialize the template engine
clearCache()  : bool
Clear the template cache
getAvailableTemplates()  : array<string|int, mixed>
Get the list of available templates
log()  : void
registerFilter()  : self
Register a custom template filter
render()  : string|bool
Render a template with the provided data
retrieveTemplate()  : string
setCacheLifetime()  : self
Set the cache lifetime in seconds
setDebugMode()  : void
Set the cache On/Off
compileTemplate()  : string
Compile template content to PHP
getTemplate()  : string
Get template content from file
parseCondition()  : string
Parse condition expressions to properly handle variables
parseIncludeParams()  : array<string|int, mixed>
Parse parameters for include statements
resolveTemplatePath()  : string
Resolve the template path from a template name

Properties

$compiledTemplate

private array<string|int, mixed>|bool $compiledTemplate = []

$logger

private object|null $logger = null

$loopVariables

private array<string|int, mixed> $loopVariables = []

Methods

__construct()

Initialize the template engine

public __construct(string $templateDir, string $cacheDir[, bool $debug = false ]) : mixed
Parameters
$templateDir : string

Directory containing template files

$cacheDir : string

Directory for compiled template cache

$debug : bool = false

Whether to enable debug mode (disables caching)

clearCache()

Clear the template cache

public clearCache([string|null $template = null ]) : bool
Parameters
$template : string|null = null

Specific template to clear from cache, or null for all

Return values
bool

True if cache was cleared successfully

getAvailableTemplates()

Get the list of available templates

public getAvailableTemplates() : array<string|int, mixed>
Return values
array<string|int, mixed>

List of template names without extension

log()

public log([mixed $message = '' ][, string $level = 'info' ][, mixed $backtrace = null ][, int $spacing = 0 ]) : void
Parameters
$message : mixed = ''
$level : string = 'info'
$backtrace : mixed = null
$spacing : int = 0

registerFilter()

Register a custom template filter

public registerFilter(string $name, callable $callback) : self
Parameters
$name : string

Filter name

$callback : callable

Filter function

Return values
self

render()

Render a template with the provided data

public render(string $template[, array<string|int, mixed> $data = [] ][, bool $print = true ]) : string|bool
Parameters
$template : string

Template name (without extension)

$data : array<string|int, mixed> = []

Data to pass to the template

$print : bool = true
Tags
throws
Exception

If template rendering fails

Return values
string|bool

Rendered template

retrieveTemplate()

public retrieveTemplate(string $template) : string
Parameters
$template : string
Return values
string

setCacheLifetime()

Set the cache lifetime in seconds

public setCacheLifetime(int $seconds) : self
Parameters
$seconds : int

Cache lifetime in seconds

Return values
self

setDebugMode()

Set the cache On/Off

public setDebugMode(bool $debug) : void
Parameters
$debug : bool

Enablo or disable Caching

compileTemplate()

Compile template content to PHP

private compileTemplate(string $content, string $currentTemplate) : string
Parameters
$content : string

Template content

$currentTemplate : string

Current template name for error reporting

Return values
string

Compiled PHP code

getTemplate()

Get template content from file

private getTemplate(string $templatePath) : string
Parameters
$templatePath : string

Full path to template file

Tags
throws
RuntimeException

If template file doesn't exist

Return values
string

Template content

parseCondition()

Parse condition expressions to properly handle variables

private parseCondition(string $condition) : string
Parameters
$condition : string

The condition from the template

Return values
string

PHP-compatible condition

parseIncludeParams()

Parse parameters for include statements

private parseIncludeParams(string $paramsString) : array<string|int, mixed>
Parameters
$paramsString : string

String containing parameters (key=value, key2="value2")

Return values
array<string|int, mixed>

Associative array of parameters

resolveTemplatePath()

Resolve the template path from a template name

private resolveTemplatePath(string $template) : string
Parameters
$template : string

Template name

Return values
string

Full template path


        
On this page

Search results