VELOX API Docs

Engine
in package

A class that serves as a templating engine for view files.

This templating engine is based on regular expression.

Templating tags:

  • Create a block {! @block name !} ... {! @endblock !}, use {! @super !} to inherit parent block.
  • Print a block {! @block(name) !}.
  • Extend a file {! @extends 'theme/layouts/file' !}, blocks of this file will be inherited.
  • Include a file {! @include 'theme/includes/file' !}, this will get rendered before inclusion.
  • Embed a file {! @embed 'theme/components/file' !}, this will be included as is.
  • Control structures {! @if ($var) !} ... {! @endif !}, {! @foreach($vars as $var) !} ... {! @endforeach !}.
  • Variable assignments {! $var = '' !}, content can be a variable or any valid PHP expression.
  • Print a variable {{ $var }}, content can be a variable or any PHP expression that can be casted to a string.
  • Print a variable without escaping {{{ $var }}}, content can be a variable or any PHP expression that can be casted to a string.
  • Comment something {# This is a comment #}, this will be a PHP comment (will not be available in final HTML).
Tags
since
1.3.0

Table of Contents

REGEX  = ['dependency' => '/{!\s*@(extends|embed)\s+['"]?(.*?)['"]?\s*!}/s', 'include' => '/{!\s*@include\s+['"]?(.*?)['"]?\s*!}/s', 'block' => '/{!\s*@block\s+(.*?)\s*!}(.*?){!\s*@endblock\s*!}(?!.+{!\s*@endblock\s*!})/s', 'block.child' => '/{!\s*@block\s+.*?\s*!}.*({!\s*@block\s+(.*?)\s*!}(.*?){!\s*@endblock\s*!}).*{!\s*@endblock\s*!}/s', 'block.print' => '/{!\s*@block\((.*?)\)\s*!}/s', 'block.super' => '/{!\s*@super\s*!}/s', 'php' => '/{!\s*(.+?)\s*!}/s', 'comment' => '/{#\s*(.+?)\s*#}/s', 'controlStructure' => '/@(if|else|elseif|endif|do|while|endwhile|for|endfor|foreach|endforeach|continue|switch|endswitch|break|return|require|include)/s', 'controlStructure.start' => '/^@(if|else|elseif|while|for|foreach|switch)/s', 'controlStructure.end' => '/@(endif|endwhile|endfor|endforeach|endswitch)$/s', 'print' => '/{{\s*(.+?)\s*}}/s', 'print.unescaped' => '/{{{\s*(.+?)\s*}}}/s']
Regular expressions for syntax tokens.
$debug  : bool
Whether or not to add debugging info to the compiled template.
$cache  : bool
Whether or not to cache compiled template files.
$cacheDirectory  : string
Template files cache directory.
$templatesDirectory  : string
Template files directory.
$templatesFileExtension  : string
Template files file extension.
$blocks  : array<string|int, mixed>
Currently captured template blocks.
__construct()  : mixed
Class constructor.
clearCache()  : void
getCompiledContent()  : string
Compiles a template file and returns the result after compilation.
getCompiledFile()  : string
Compiles a template file and returns the path to the compiled template file from cache directory.
getEvaluatedContent()  : string
Evaluates a template file after compiling it in a temporary file and returns the result after evaluation.
isDebug()  : bool
Get the value of `static::$debug`.
render()  : void
Renders a template file and pass the passed variables to it.
setDebug()  : $this
Set the value of `static::$debug`
extractBlocks()  : string
Extract blocks data from template code.
importDependencies()  : string
Imports template dependencies.
importIncludes()  : string
Imports template includes.
injectBlocks()  : string
Injects blocks data in template code.
parseBlock()  : string
Parses a template block, extract data from it, and updates class internal state.
printVariables()  : string
Echos escaped and unescaped variables in template code.
require()  : void
Requires a PHP file and pass it the passed variables.
resolveCachePath()  : string
Resolves a template file path from cache directory.
resolvePath()  : string
Resolves a template file path.
wrapComments()  : string
Wraps comments in template code.
wrapControlStructures()  : string
Wraps control structures and PHP code in template code.
wrapPhp()  : string
Wraps PHP in template code.
assertFileExists()  : void
Asserts that a file exists.
createCacheDirectory()  : void
Creates cache directory if it does not exist.

Constants

REGEX

Regular expressions for syntax tokens.

public mixed REGEX = ['dependency' => '/{!\s*@(extends|embed)\s+['"]?(.*?)['"]?\s*!}/s', 'include' => '/{!\s*@include\s+['"]?(.*?)['"]?\s*!}/s', 'block' => '/{!\s*@block\s+(.*?)\s*!}(.*?){!\s*@endblock\s*!}(?!.+{!\s*@endblock\s*!})/s', 'block.child' => '/{!\s*@block\s+.*?\s*!}.*({!\s*@block\s+(.*?)\s*!}(.*?){!\s*@endblock\s*!}).*{!\s*@endblock\s*!}/s', 'block.print' => '/{!\s*@block\((.*?)\)\s*!}/s', 'block.super' => '/{!\s*@super\s*!}/s', 'php' => '/{!\s*(.+?)\s*!}/s', 'comment' => '/{#\s*(.+?)\s*#}/s', 'controlStructure' => '/@(if|else|elseif|endif|do|while|endwhile|for|endfor|foreach|endforeach|continue|switch|endswitch|break|return|require|include)/s', 'controlStructure.start' => '/^@(if|else|elseif|while|for|foreach|switch)/s', 'controlStructure.end' => '/@(endif|endwhile|endfor|endforeach|endswitch)$/s', 'print' => '/{{\s*(.+?)\s*}}/s', 'print.unescaped' => '/{{{\s*(.+?)\s*}}}/s']

Properties

$debug

Whether or not to add debugging info to the compiled template.

public static bool $debug = false

$cache

Whether or not to cache compiled template files.

protected bool $cache

$cacheDirectory

Template files cache directory.

protected string $cacheDirectory

$templatesDirectory

Template files directory.

protected string $templatesDirectory

$templatesFileExtension

Template files file extension.

protected string $templatesFileExtension

$blocks

Currently captured template blocks.

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

Methods

__construct()

Class constructor.

public __construct([string $templatesDirectory = './templates' ][, string $templatesFileExtension = '.phtml' ][, string $cacheDirectory = './cache/' ][, bool $cache = true ][, bool $debug = false ]) : mixed
Parameters
$templatesDirectory : string = './templates'
$templatesFileExtension : string = '.phtml'
$cacheDirectory : string = './cache/'
$cache : bool = true
$debug : bool = false
Return values
mixed

clearCache()

public clearCache() : void
Return values
void

getCompiledContent()

Compiles a template file and returns the result after compilation.

public getCompiledContent(string $file) : string
Parameters
$file : string

A relative path to template file from templates directory.

Tags
throws
Exception

If file does not exist.

Return values
string

getCompiledFile()

Compiles a template file and returns the path to the compiled template file from cache directory.

public getCompiledFile(string $file) : string
Parameters
$file : string

A relative path to template file from templates directory.

Tags
throws
Exception

If file does not exist.

Return values
string

getEvaluatedContent()

Evaluates a template file after compiling it in a temporary file and returns the result after evaluation.

public getEvaluatedContent(string $file[, array<string|int, mixed> $variables = [] ]) : string
Parameters
$file : string

A relative path to template file from templates directory.

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

The variables to pass to the template.

Tags
throws
Exception

If file does not exist.

Return values
string

isDebug()

Get the value of `static::$debug`.

public isDebug() : bool
Return values
bool

render()

Renders a template file and pass the passed variables to it.

public render(string $file[, array<string|int, mixed> $variables = [] ]) : void
Parameters
$file : string

A relative path to template file from templates directory.

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

The variables to pass to the template.

Return values
void

setDebug()

Set the value of `static::$debug`

public setDebug(bool $debug) : $this
Parameters
$debug : bool
Return values
$this

extractBlocks()

Extract blocks data from template code.

protected final extractBlocks(string $code) : string
Parameters
$code : string
Return values
string

importDependencies()

Imports template dependencies.

protected final importDependencies(string $file) : string
Parameters
$file : string

The template file.

Tags
throws
Exception

If file does not exist.

Return values
string

importIncludes()

Imports template includes.

protected final importIncludes(string $code) : string
Parameters
$code : string

The template code.

Return values
string

injectBlocks()

Injects blocks data in template code.

protected final injectBlocks(string $code) : string
Parameters
$code : string
Return values
string

parseBlock()

Parses a template block, extract data from it, and updates class internal state.

protected final parseBlock(string $code) : string
Parameters
$code : string

The template block.

Return values
string

printVariables()

Echos escaped and unescaped variables in template code.

protected final printVariables(string $code) : string
Parameters
$code : string
Return values
string

require()

Requires a PHP file and pass it the passed variables.

protected static require(string $file[, array<string|int, mixed>|null $variables = null ]) : void
Parameters
$file : string

An absolute path to the file that should be compiled.

$variables : array<string|int, mixed>|null = null

[optional] An associative array of the variables to pass.

Return values
void

resolveCachePath()

Resolves a template file path from cache directory.

protected resolveCachePath(string $file) : string
Parameters
$file : string

The file path to resolve.

Return values
string

resolvePath()

Resolves a template file path.

protected resolvePath(string $file) : string
Parameters
$file : string

The file path to resolve.

Return values
string

wrapComments()

Wraps comments in template code.

protected final wrapComments(string $code) : string
Parameters
$code : string
Return values
string

wrapControlStructures()

Wraps control structures and PHP code in template code.

protected final wrapControlStructures(string $code) : string
Parameters
$code : string
Return values
string

wrapPhp()

Wraps PHP in template code.

protected final wrapPhp(string $code) : string
Parameters
$code : string
Return values
string

assertFileExists()

Asserts that a file exists.

private assertFileExists(string $file) : void
Parameters
$file : string

An absolute path to a file.

Tags
throws
Exception

If file does not exist.

Return values
void

createCacheDirectory()

Creates cache directory if it does not exist.

private createCacheDirectory() : void
Return values
void

Search results