A simple, micro template engine for PHP
A simple, micro template engine for PHP, (*1)
Loading your template:, (*2)
setTemplate( 'event.php' ); // Pass variables to your template $x->setContext( [ 'title' => 'County Fair', 'venue' => 'County Fairgrounds', ] ); // Render the template echo $x->render(); ``` The template file: ```phpget('title'); ?>
Venue: get('venue', 'To Be Determined'); ?>
Check if a variable has a specific value., (*3)
$x->is( $name, $value );Params:, (*4)
- string - Name of the variable to check.
- mixed - Value to check.
Return Value:, (*5)
boolean, (*6)
Description:, (*7)
The
is()
method allows you to check if a variable in the template's context contains a specific value., (*8)
Check if a variable exists., (*9)
$x->has( $name );Params:, (*10)
- string|array - Name of the variable to check, or a path to a nested value.
Return Value:, (*11)
boolean, (*12)
Description:, (*13)
The
has()
method allows you to check if a variable exists in the template's context. You can pass in a variable name, or a path to a nested data structure. For example, you can pass in an array, or use dot notation (e.g.var.0.title
will look for a variable namedvar
, find the first item in the array, and then find the title property from the object)., (*14)
Check if a value exists in an object or an array., (*15)
$x->hasIn( $data, $name );Params:, (*16)
- object|array - Data from which to locate value.
- string|array - Name of variable, or nested path.
Return Value:, (*17)
boolean, (*18)
Description:, (*19)
The
hasIn()
method is the same as thehas()
method, except you can pass in your own data as opposed to defaulting to the template's context., (*20)
Get a variable., (*21)
$x->get( $name, $default );Params:, (*22)
- string|array - Name (or path) of the variable to be fetched.
- string|array - The default value to be returned if variable doesn't exist.
Return Value:, (*23)
mixed, (*24)
Description:, (*25)
The
get()
method allows you to fetch a value from the template's context. You can pass in a variable name, or a path to a nested data structure. For example, you can pass in an array, or use dot notation (e.g.var.0.title
to get the title property from the first value in the array contained in the variable namedvar
.) Optionally, you can pass in a default value to be returned if the variable you are asking for isn't defined., (*26)
Get a value from an object or an array., (*27)
$x->getIn( $data, $name, $default );Params:, (*28)
- object|array - Data from which to fetch value.
- string|array - Name (or path) of the variable to be fetched.
- mixed - The default value to be returned if variable doesn't exist.
Return Value:, (*29)
mixed, (*30)
Description:, (*31)
The
getIn()
method is the same as theget()
method, except you can pass in your own data as opposed to defaulting to the template's context., (*32)
Set a variable., (*33)
$x->set( $name, $value );Params:, (*34)
- string|array - Name (or path) of the variable to be set.
- mixed - The value to be assigned.
Return Value:, (*35)
void, (*36)
Description:, (*37)
The
set()
method allows you to set a value in the template's context. You can pass in a variable name, or a path to a nested data structure. For example, you can pass in an array, or use dot notation (e.g.var.0.title
to set the title property from the first value in the array contained in the variable namedvar
.) Optionally, you can pass in a default value to be returned if the variable you are asking for isn't defined., (*38)
Set a value in an object or an array., (*39)
$x->setIn( $data, $name, $value );Params:, (*40)
- object|array - Data in which to set value.
- string|array - Name (or path) of the variable to be set.
- mixed - The value to be assigned.
Return Value:, (*41)
mixed - Returns the updated data or original data on failure., (*42)
Description:, (*43)
The
setIn()
method is the same as theset()
method, except you can pass in your own data as opposed to defaulting to the template's context., (*44)
Unset a variable., (*45)
$x->delete( $name );Params:, (*46)
- string|array - Name of the variable to be deleted.
Return Value:, (*47)
void, (*48)
Description:, (*49)
The
delete()
method will remove a variable from the template's context., (*50)
Loads a template from within an existing template., (*51)
$x->load( $template, $vars, $withContext = true );Params:, (*52)
- string - Relative path to the template to be loaded.
- array - An associative array containing variable names and values to pass to the template.
- boolean - Whether or not to keep the variables from the parent template's context.
Return Value:, (*53)
void, (*54)
Description:, (*55)
The
load()
method will output a child template within a template., (*56)
Render a template to a string., (*57)
$x->render();Params:, (*58)
none, (*59)
Return Value:, (*60)
string, (*61)
Description:, (*62)
The
render()
method will return a string containing the rendered template., (*63)
Set the template to be rendered., (*64)
$x->setTemplate( $template );Params:, (*65)
string - Relative path to the template to be rendered., (*66)
Return Value:, (*67)
void, (*68)
Description:, (*69)
The
setTemplate()
method is how you set the template that you want to render. It will be the relative path to the template (e.g.event.php
)., (*70)
Set the context., (*71)
$x->setContext( $vars );Params:, (*72)
array - An associative array containing variable names and values to pass to the template., (*73)
Return Value:, (*74)
void, (*75)
Description:, (*76)
The
setContext()
method allows you to pass variables to the template's context. Any variables passed will be made available to the template being rendered., (*77)
Set the template paths., (*78)
$x->setTemplatePaths( $templatePaths );Params:, (*79)
array - An array of template paths., (*80)
Return Value:, (*81)
void, (*82)
Description:, (*83)
The
setTemplatePaths()
method allows you to define one or more template paths to check when attempting to load a template., (*84)
Add a template path., (*85)
$x->addTemplatePath( $templatePath );Params:, (*86)
string - A template path to append to the list of template paths., (*87)
Return Value:, (*88)
void, (*89)
Description:, (*90)
The
addTemplatePath()
method allows you to append a template path to the list of template paths to check when attempting to load a template., (*91)