Freeocart

Free Opencart ecosystem initiative

FOC:CSV embedded template system – how it's works

Short how-to for FOC:CSV template system.

Hi pals! FOC:CSV module has embedded template system for feature known as “multicolumn parser”.

Multicolumn parser is simple function, that enables you to combine multiple CSV columns content and render result you want to produce into one database cell, using simple template.

At the moment, templating system supports variable interpolation, simple loops and using some allowed functions.

We will cover every of these possibilities, and at the end i will show how you can get and use templater for your own projects (but i’m not recommend to do it – template system very limited)

Variables

To output variable value, use classic TWIG-like syntax − {{ VARIABLE }}. If variable is scalar, text or number, for example − it will displayed as is. In case you pass there array or object, system will display JSON representation.

Loop

Loops used with array/hashes and have next syntax:

[@each (value,index) <= source]
// loop body, for example {{ index }}
[@endeach]

// or
[@each value <= source]
// loop body
[@endeach]

Loop indexation starts with 1, you can get current index using second variable declared in @each (index variable) or using special loop variable using loop.index.

Functions

Function call syntax looks like this:

[@fn FUNCTION_NAME | @argument] or [@fn FUNCTION_NAME | "Value"]

Take attention, syntax with at symbol (@) used for variables, but you also can put your defined value:

[@fn date | "Y-m-d"] will be processed as - date("Y-m-d")

[@fn md5 | @variable] will be processed as - md5($variables['variable'])

Allowed (builtin) functions list:

List of allowed functions placed in protected attribute enabled_functions of FocSimpleTemplater class. You can customize the list using OCMod.

Using templater class in your own projects

Templater is very simplified DSL abstraction around PHP, implemented with regular expressions, so, maybe it would'nt be a good idea to use it in your projects, in case you want render some heavy views or complex data.

But, if you really want to use FOCSimpleTemplater, you need only connect templater class (system/library/FocSimpleTemplater.php), by using $this->load->library('FocSimpleTemplater') and call static method render, with template string in first argument and template variables hash in second:

$variables = array('hello' => 'world', 'arr' => array('arr1', 'arr2'));
$template = <<