Syntax

1. Getting started
MoxesTemplate can be called in three ways.

First is when simply give the template file:
<?php
require_once('MoxesTemplate.php');

$tpl = new MoxesTemplate('path/example.tpl');
....
?>


The second way is to pass a path. When a path is given, you cannot include files outside of this path.
<?php
require_once('MoxesTemplate.php');

$tpl = new MoxesTemplate(array('path'=>'some/path','filename'=>'template.tpl'));
....
?>


The third way is to pass a html code.
<?php
require_once('MoxesTemplate.php');

$tpl = new MoxesTemplate(array('html'=>'<html> ...'));
....
?>


Other parameters:
<?php
require_once('MoxesTemplate.php');

$tpl = new MoxesTemplate(array(
'html'=>'<html> ...',
'opentag' => '[',
'closetag' => ']'
));
....
?>
2. Passing parameters
You can pass parameters using param(). For example:
$tpl->param('name', 'George');


Or pass an array:
$names = array( 'George', 'Ivan');

$tpl->param('names', $names );
3. Passing function references
You can also proceed a function to the template.
For example if you want to make translations in the template you can do:
$names = array(
'name' => 'George',
'lastname' => 'Georgiev'
);

function getName( $name ) {
global $names;
return $names[$name];
}

$tpl->func( 'username', 'getName');


Then in the template you can call:
{username "lastname"}

The following example will return: Georgiev
4. Printing parsed template
To print the parsed template you just need to do:
print $tpl->output();
5. Template syntax
The avaible blocks are described bellow.
5.1. Variable
Reading scalar variable:
The name of the person is {$name}.


To read a key from a passed array:
The last name of the person is {$names.lastname}.
5.2. Special variable
Special variables (require version >=1.8) are variables that process other variables or are used for checkers. The special variable is passed as param:

<?php
$tpl->sysparam('NAME_OF_VAR', 'CALL_FUNC' ); // CALL_FUNC can be array( $obj, 'method )
?>


To this CALL_FUNC are passed 3 parameters:
CALL_FUNC( $after, $before, $template_object, $all );

# $all contents
# in case: $something.is.called.twise
# $all = array(
# 'before' => array( 0 => 'something' ),
# 'current' => 'is',
# 'after' => array( 0 => 'called', 1 => 'twise' )
# )

Example usage:

// PHP Code
<?php

$tpl->param('something', 'YES');
$tpl->sysparam('is', 'available' );

function available( $after, $before, $template ) {
if ( isset( $before ) ) {
return $before == $after ? true : false;
} else {
return $template->params['something'] == 'ok' ? true : false;
}
}
?>


Template code:

{if $something.is.ok}
IT IS OK
{/if}

{if $is.ok}
IT IS OK
{/if}
5.3. if
The syntax of the "if" block is as fallows:
....
The boy is {if $age >= "18"}over{else}under{/if}
....


The if statement can be for variables only or variables and strings. The string no matter is it integer or letters is always in double quotes.

Here is a full list of statement examples:
...
{if $floor == "10"} same as {if $floor == 10 }
...
{if $floor != "10"}
...
{if $floor >= "10"}
...
{if $floor <= "10"}
...
{if $floor > "10"}
...
{if $floor < "10"}
...
{if $floor}
...
{if !$floor}
...
{if $floor % "10"}
...
{if $floor !% "10"}
...
{if $variable == $other}
...
{if math( 6 / 3 ) % 3}
...
I don't think those examples need more explanations.

The {elseif ...} block is the same as the "if" block. There is also {else} and {/if} for closing the if block.

Simple example:
{if $age < "18"}
You're under age
{elseif $age > "18"}
You're over eighteen.
{else}
You're exactly on eighteen.
{/if}
5.4. loop
The loop block is called with only one parameter which has to be an array.
I like to eat
{loop $fruit}
$value
{/loop}


Or can be called with an array of arrays:
My friends are:
{loop $friends}
{$value.name} {$value.lastname}
{/loop}


Loops can be called inside other loops:
My friends are:
{loop $friends}
{$value.name} {$value.lastname} and he likes to eat
{loop $value.fruit}
{$value}
{/loop}
{/loop}


The loop block generates two parameters from the array: $key and $value.

Other parameters that the loop block generates automaticaly are:
$count - the number of the element counting from 1
$__isFirst - true if the element is first in the array
$__isLast - true if the element is last in the array
$__isOdd - if the element is odd
$__prevKey - previous $key
$__prevValue - previous $value
$prev - previous $value
$prevKey - previous $key
$next - next $value
$nextKey - next $key
$parent - parent values array if loop is nested (Example: $parent.value.some or $parent.count)
5.5. include
You can include other templates or files in the template
{include "other.tpl"}


If you initiate MoxesTemplate with 'path' option, then inside the include block can be included only templates from this path. Every other attempt will couse error for non existing file.
5.6. functions
When you pass a reference to a function, you create a new block. For example if you pass:
<?php
$tpl->func('translate', 'tr')
?>


This will create block {translate "...."} and the second parameter in the block will be passed to the function "tr" in the php.
5.7. math
Need to do calculations:
{math 1 + $var / 2}
Author: Milen Hristov
Last revision: 27.12.2015 20:28