stupid_template_engine.php

The implementation of the Stupid Template Engine.

Summary
stupid_template_engine.phpThe implementation of the Stupid Template Engine.
LicenseThis file is licensed under the MIT/X11 License.
steEverything in this file is in this namespace.
Functions
precompilePrecompiling STE T/PL templates.
parseParsing a STE T/PL template.
transcompileTranscompiles an abstract syntax tree to PHP.
Constants
Template modes
StorageAccessAn interface.
Functions
loadLoading a template.
saveSaves a template.
FilesystemStorageAccessThe default StorageAccess implementation for loading / saving templates into a directory structure.
Functions
__construct
STECoreThe Core of STE
Variables
Public variables
Functions
__construct
register_tagRegister a custom tag.
call_tagCalling a custom tag (builtin ones can not be called)
exectemplateExecutes a template and returns the result.
get_var_referenceGet a reference to a template variable using a variable name.
set_var_by_nameSet a template variable by its name.
get_var_by_nameGet a template variable by its name.
loadLoad a template and return its result (blocks not included, use exectemplate for this).
evalboolTest, if a text represents false (an empty / only whitespace text) or true (everything else).

License

This file is licensed under the MIT/X11 License.  See COPYING for more details.

ste

Everything in this file is in this namespace.

Summary
Functions
precompilePrecompiling STE T/PL templates.
parseParsing a STE T/PL template.
transcompileTranscompiles an abstract syntax tree to PHP.
Constants
Template modes

Functions

precompile

function precompile($code)

Precompiling STE T/PL templates.  You only need this function, if you want to manually transcompile a template.

Parameters

$codeThe input code

Returns

The precompiled code.

parse

function parse($code,
$tpl)

Parsing a STE T/PL template.  You only need this function, if you want to manually transcompile a template.

Parameters

$codeThe STE T/PL code.
$tplThe name of the template.

Returns

An abstract syntax tree, which can be used with transcompile.

transcompile

function transcompile($ast) /* Transcompile and add some boilerplate code. */

Transcompiles an abstract syntax tree to PHP.  You only need this function, if you want to manually transcompile a template.

Parameters

$astThe abstract syntax tree to transcompile.

Returns

PHP code.  The PHP code is an anonymous function expecting a STECore instance as its parameter and returns a string (everything that was not pached into a section).

Constants

Template modes

MODE_SOURCEThe Templates source
MODE_TRANSCOMPILEDThe transcompiled template

StorageAccess

An interface.  A StorageAccess implementation is used to access the templates from any storage.  This means, that you are not limited to store the Templates inside directories, you can also use a database or something else.

Summary
Functions
loadLoading a template.
saveSaves a template.

Functions

load

public function load($tpl,
&$mode)

Loading a template.

Parameters

$tplThe name of the template.
&$modeWhich mode is preferred?  One of the <Template modes>.  If <MODE_SOURCE>, the raw sourcecode is expected, if <MODE_TRANSCOMPILED> the transcompiled template as a callable function (expecting an STECore instance as first parameter) is expected.  If the transcompiled version is not available or older than the source, you can set this parameter to <MODE_SOURCE> and return the source.

Returns

Either the sourcecode or a callable function (first, and only parameter: an STECore instance).

save

public function save($tpl,
$data,
$mode)

Saves a template.

Parameters

$tpl -The name of the template.  $data - The data to be saved.  $mode - A <Template mode> constant.

FilesystemStorageAccess

The default StorageAccess implementation for loading / saving templates into a directory structure.

Functions

__construct

public function __construct($src,
$transc)

Parameters

$srcThe directory with the sources (Writing permissions are not mandatory, because STE does not save template sources).
$transcThe directory with the transcompiled templates (the PHP instance / the HTTP Server needs writing permissions to this directory).

STECore

The Core of STE

Summary
Variables
Public variables
Functions
__construct
register_tagRegister a custom tag.
call_tagCalling a custom tag (builtin ones can not be called)
exectemplateExecutes a template and returns the result.
get_var_referenceGet a reference to a template variable using a variable name.
set_var_by_nameSet a template variable by its name.
get_var_by_nameGet a template variable by its name.
loadLoad a template and return its result (blocks not included, use exectemplate for this).
evalboolTest, if a text represents false (an empty / only whitespace text) or true (everything else).

Variables

Public variables

$blocksAssociative array of blocks (see the language definition).
$blockorderThe order of the blocks (an array)
$varsAssociative array of all template variables.  Use this to pass data to your templates.

Functions

__construct

public function __construct($storage_access)

Parameters

$storage_accessAn Instance of a StorageAccess implementation.

register_tag

public function register_tag($name,
$callback)

Register a custom tag.

Parameters

$nameThe name of the tag.
$callbackA callable function (This must tage three parameters: The STECore instance, an associative array of parameters, and a function representing the tags content(This expects the STECore instance as its only parameter and returns its text result, i.e to get the text, you neeed to call this function with the STECore instance as a parameter)).

call_tag

public function call_tag($name,
$params,
$sub)

Calling a custom tag (builtin ones can not be called)

Parameters

$nameThe Tag’s name
$paramsAssociative array of parameters
$subA callable function (expecting an STECore instance as it’s parameter) that represents the tag’s content.

Returns

The output of the tag.

exectemplate

public function exectemplate($tpl)

Executes a template and returns the result.  The huge difference to load is that this function will also output all blocks.

Parameters

$tplThe name of the template to execute.

Returns

The output of the template.

get_var_reference

public function &get_var_reference($name,
$create_if_not_exist)

Get a reference to a template variable using a variable name.  This can be used,if your custom tag takes a variable name as a parameter.

Parameters

$nameThe variables name.
$create_if_not_existShould the variable be created, if it does not exist?  Otherwise NULL will be returned, if the variable does not exist.

Returns

A Reference to the variable.

set_var_by_name

public function set_var_by_name($name,
$val)

Set a template variable by its name.  This can be used,if your custom tag takes a variable name as a parameter.

Parameters

$nameThe variables name.
$valThe new value.

get_var_by_name

public function get_var_by_name($name)

Get a template variable by its name.  This can be used,if your custom tag takes a variable name as a parameter.

Parameters

$nameThe variables name.

Returns

The variables value.

load

public function load($tpl,  
$quiet = False)

Load a template and return its result (blocks not included, use exectemplate for this).

Parameters

$tplThe name of the template to be loaded.
$quietIf true, do not output anything and do notmodify the blocks.  This can be useful to load custom tags that are programmed in STE T/PL.  Default: false.

Returns

The result of the template (if $quiet == false).

evalbool

public function evalbool($txt)

Test, if a text represents false (an empty / only whitespace text) or true (everything else).

Parameters

$txtThe text to test.

Returns

true/false.

function precompile($code)
Precompiling STE T/PL templates.
function parse($code,
$tpl)
Parsing a STE T/PL template.
function transcompile($ast) /* Transcompile and add some boilerplate code. */
Transcompiles an abstract syntax tree to PHP.
public function load($tpl,
&$mode)
Loading a template.
public function save($tpl,
$data,
$mode)
Saves a template.
An interface.
public function __construct($src,
$transc)
public function __construct($storage_access)
public function register_tag($name,
$callback)
Register a custom tag.
public function call_tag($name,
$params,
$sub)
Calling a custom tag (builtin ones can not be called)
public function exectemplate($tpl)
Executes a template and returns the result.
public function &get_var_reference($name,
$create_if_not_exist)
Get a reference to a template variable using a variable name.
public function set_var_by_name($name,
$val)
Set a template variable by its name.
public function get_var_by_name($name)
Get a template variable by its name.
public function load($tpl,  
$quiet = False)
Load a template and return its result (blocks not included, use exectemplate for this).
public function evalbool($txt)
Test, if a text represents false (an empty / only whitespace text) or true (everything else).
The Core of STE
Close