template class

version : 1.0
source : template.class.php

Templates allow for PHP processing and the resulting (html/text) output to have a layer of abstraction.
The way the templates work, macros are defined where dynamic data needs to be placed.
Macros are used for variable insertion, commands and control statements.

Template code:

Macros:

     These are defined by the macro variable name in curly brackets.      Arrays can use . or / to navigate
     Variables:
          {MacroVariable}
     Arrays:
          {MacroArray/NestedArr/Key}
          {MacroArray.NestedArr.Key}

Commands:

     These are defined with the command name and a set of parentheses (for adding parameters) and surrounded by curly brackets.
     Parameters go inside the parentheses and are separated by commas
     Parameters without single quotes are considered macro replacements
          {command (MacroParam, 'StaticParam', MacroParam2)}

Control statements:

     These are defined with the statement name followed by macros in curly brackets followed by
     an end tag with a backslash and the statement name again surrounded by curly brackets
     IF Statement:
          {if (BoolMacro)}
               Data to include if BoolMacro is true
          {/if}
          {if (!BoolMacro)}
               Data to include if BoolMacro is false
          {/if}
     FOR Loop:
          {for (10) as (incVar)}
               Data to be printed 10 times {incVar}: {MacroArray.incVar.data}
          {/for}
          {for ( {count (MacroX)} ) as (incVar)}
               Data to be printed {MacroX} times {incVar}: {MacroArray.incVar.data}
          {/for}
     FOREACH Loop:
          {foreach (MacroArray) as (key)}
               Data to be printed for each key in array {key}: {MacroArray.key.data}
          {/foreach}

PHP Code:

1. Initialize template object

     $template = new template([string $template = null, array $macroArray = null]);

Parameters:
     $template = FileName of template to process
     $macroArray = Array of macro replacements for template

2. Add necessary template commands

     $template->add_command(string $commandName, function $command);

Parameters:
     $commandName = Name of command to register
     $command = callback function for template

3. Load contents of template file

     $template->load(string $template);

Parameters:
     $template = FileName of template to process
Returns:
     Contents of loaded file as a string

4. Set object template to render

     $template->content = (string templateContent);

5. Add Macros to be run on the template, will overwrite all previously added macros

     $template->set_macros(array $macroArray);

Parameters:
     $macroArray = Array of macro replacements for template

6. Add Macros to be run on the template, will only overwrite previously added macros with the same name

     $template->add_macro(string/int $key, mixed $value);

Parameters:
     $key = Name of Macro to register
     $value = Value of Macro to register

7. Render the template with the macros for outputting

     $template->render([string $content = null, array $macroArray = null]);

Parameters:
     $content = Template data to replace macros in
     $macroArray = Array of macro replacements for template