Template Parser
The template parser parses file contents and replaces the template variables with their values. It supports sections, the sections could be nested. The number of times a section is shown is controlled from the PHP code. There is no logic in the template file.
It is convention, but not a requirement, to use template variables in the form %%VAR%%
. This helps to spot the template variables easier in the template file.
* Example:
<?php
$t = new miTemplateParser();
$t->readTemplate('template.tmpl');
$t->assign('%%VAR%%', 'test value');
$t->templateShow();
?>
Template:
<span style="color: #ff0000">%%VAR%%</span>
Result:
<span style="color: #ff0000">test value</span>
Subsection example:
<?php
$section = new miTemplateParserSectionInfo();
$section->setSectionInfo('test1', 1, array('%%VAR1%%' => 'a variable'));
$t = new miTemplateParser();
$t->setSectionInfos(array($section));
$t->templateShow();
?>
Template: <mi:section name="test1">%%VAR1%%</mi:section>
Result:
a variable
* Multiple subsections example:
<?php
$section = new miTemplateParserSectionInfo();
$section->setSectionInfo('test4', 1);
$subSectionA = new miTemplateParserSectionInfo();
$subSectionA->setSectionInfo('subtest4a', 1, array('%%SUBSECTION_VAR%%' => 'subsection var'));
$subSectionB = new miTemplateParserSectionInfo();
$subSectionB->setSectionInfo('subtest4b', 1);
$section->addSubsection($test4SubSectionA);
$section->addSubsection($test4SubSectionB);
$t = new miTemplateParser();
$t->setSectionInfos(array($section));
$t->templateShow();
?>
Template:
<mi:section name="test4">
<p><mi:section name="subtest4a">%%SUBSECTION_VAR%%</mi:section></p>
<p><mi:section name="subtest4b">subsection 4b</mi:section></p>
</mi:section>
Result:
<p>subsection var</p>
<p>subsection 4b</p>