Source for file TemplateParser.php
Documentation is available at TemplateParser.php
* @copyright Copyright (c) 2003-2007 Mirchev Ideas Ltd. All rights reserved.
* Template parser section
* @copyright Copyright (c) 2003-2007 Mirchev Ideas Ltd. All rights reserved.
* Associative array with the subsections. The key is the subsection name
* All params are optional
* @see setSectionInfo for an example
* @param string $sectionName the name of the section as named in the template
* @param int $iterationsCount the number of times this section will be repeated
* @param array $values array of arrays with the values
public function __construct($sectionName =
'', $iterationsCount =
1, $values =
array())
* 'VariableName' => 'VariableValue',
* 'VariableName2' => 'VariableValue2',
* 'VariableName3' => array('Value1', 'Value2', 'Value3', 'Value4'),
* setSectionInfo('testSection', 4, $values);
* @param string $sectionName the name of the section as named in the template
* @param int $iterationsCount the number of times this section will be repeated
* @param array $values array of arrays with the values
public function setSectionInfo($sectionName, $iterationsCount, $values =
array())
* @param string $sectionName the name of the section as named in the template
* Set the number of times this section will be repated
* @param int $iterationsCount the number of times this section will be repeated
* Sets the section values
* @see setSectionInfo for more info
* @param array $values array of arrays with the values
* @see setSectionInfo for more info
* @param array $values array of arrays with the values
* Adds a subsection to this section.
* In the template the subsection must be inside this section
* @param miTemplateParserSectionInfo $subsection
public function addSubsection(miTemplateParserSectionInfo $subsection)
$this->_subsections[$subsection->_sectionName] =
$subsection;
* The number of subsections must match the number of iterations of the outer section
* @param string $subsectionName
* @param array $subsections
* 1. create a template parser object
* 2. add template parser section infos (optional step)
* 3. use readTemplate() ot setContents()
* 4. assign template variables
* @copyright Copyright (c) 2003-2006 Mirchev Ideas Ltd. All rights reserved.
const TEMPLATE_PARSER_START_TAG =
'<mi:section name="';
const TEMPLATE_PARSER_START_TAG_LEN =
'18';
const TEMPLATE_PARSER_START_TAG2 =
'">';
const TEMPLATE_PARSER_START_TAG2_LEN =
'2';
const TEMPLATE_PARSER_END_TAG =
'</mi:section>';
const TEMPLATE_PARSER_END_TAG_LEN =
'13';
const TEMPLATE_PARSER_SECTION_TAG =
'mi:section';
const TEMPLATE_PARSER_PREPROCESS_AT_ITERATIONS =
5;
* Associative array of template variables
* They will be used after all sections have been parsed
* The contents of the template file
* Reads the template file
* @param String $filename
$fd =
fopen($pathname, 'rb');
* Sets the contents of the template from string
* Alternative to reading it from file
* @param string $contents
* Parses the template and returns the result
* @return string the parsed contents
* Shows the parsed template
* @param array $templateSections
* @param array $sectionInfos
* @param int $subsectionIndex (optional)
* @return string the parsed string
foreach ($templateSections as $templateSection) {
$name =
$templateSection['name'];
// If it is an end just add the contents
$html .=
$templateSection['contents'];
$html .=
$templateSection['prefix'];
if (empty($sectionInfos[$name]))
$sectionInfo =
$sectionInfos[$name];
$sectionInfo =
$sectionInfo[$subsectionIndex];
// Check if we have subsections that are arrays
$hasSubsectionsArray =
false;
foreach ($sectionInfo->_subsections as $subsection) {
$hasSubsectionsArray =
true;
if (!$hasSubsectionsArray) {
if (isset
($templateSection['subsections']))
$contents =
$templateSection['contents'];
if ($sectionInfo->_iterationsCount >
self::TEMPLATE_PARSER_PREPROCESS_AT_ITERATIONS) {
// If more than 5 iterations do preprocessing -
// split the array variables from normal variables
$arrayValues =
$arrayKeys =
$normalValues =
$normalKeys =
array();
foreach ($sectionInfo->_values as $key =>
$r) {
for ($i =
0; $i <
$sectionInfo->_iterationsCount; $i++
) {
$replace =
$normalValues;
foreach ($arrayValues as $r)
if ($hasSubsectionsArray)
for ($i =
0; $i <
$sectionInfo->_iterationsCount; $i++
) {
foreach ($sectionInfo->_values as $r)
$replace[] =
is_array($r) ?
$r[$i] :
$r;
if ($hasSubsectionsArray and isset
($templateSection['subsections']))
* Parse the template file sections
* Parse a template section recursive method
// $nameStart = 0; // name start
// $nameEnd = 0; // This is start of end of the start tag
// $nextTagStart = 0; // The points to the current tag beginning - to the mi:section
// $pos = 0; // The end of the last tag
$nextTagStart =
strpos($theTemplateContents, self::TEMPLATE_PARSER_SECTION_TAG, $pos);
if ($nextTagStart ===
false) {
$section['name'] =
'END';
$section['contents'] =
substr($theTemplateContents, $pos);
// Check if it is an end tag
if (($nextTagStart >=
2) and substr_compare($theTemplateContents, self::TEMPLATE_PARSER_END_TAG, $nextTagStart-
2, self::TEMPLATE_PARSER_END_TAG_LEN) ==
0) {
$section['contents'] =
substr($theTemplateContents, $pos, $nextTagStart-
2 -
$pos);
$lastTagEnd =
$nextTagStart-
2 +
self::TEMPLATE_PARSER_END_TAG_LEN;
$section['name'] =
'END';
$section['contents'] =
substr($theTemplateContents, $pos, $nextTagStart-
2 -
$pos);
// Check if it is a start tag
if (($nextTagStart >=
1) and substr_compare($theTemplateContents, self::TEMPLATE_PARSER_START_TAG, $nextTagStart-
1, self::TEMPLATE_PARSER_START_TAG_LEN) ==
0) {
$section['subsections'] =
array();
// Skip past the end tag that had the subcall return
$lastTagEnd =
$pos +
self::TEMPLATE_PARSER_END_TAG_LEN;
$nameStart =
$nextTagStart-
1+
self::TEMPLATE_PARSER_START_TAG_LEN;
$nameEnd =
strpos($theTemplateContents, self::TEMPLATE_PARSER_START_TAG2, $nameStart);
if ($nameEnd ===
false) {
$name =
substr($theTemplateContents, $nameStart, $nameEnd -
$nameStart);
$section['name'] =
$name;
$section['prefix'] =
substr($theTemplateContents, $lastTagEnd, $nextTagStart-
1 -
$lastTagEnd);
$pos =
$nameEnd +
self::TEMPLATE_PARSER_START_TAG2_LEN;
* Set the section infos. Clears all previous section infos
* @param array $sectionInfos
foreach ($sectionInfos as $sectionInfo)
* If a previous section info with the same name exists it will be overwritten.
* @param miTemplateParserSectionInfo $sectionInfo
* Returns the section info with the requested name
* @param string $sectionInfoName
* @return miTemplateParserSectionInfo|falsethe section info object, or false if not found
* Assigns a variable to be replaced
* @param string $name variable name, usually %%VAR_NAME%%
* @param string $value the contents of the variable. usually escaped with htmlenities()
public function assign($name, $value)
* Assigns a array of variables to be replaced
* @param array $pairs variable name is the key, the contents is the value
* Assigns a list of variables. Both array should have equal sizes, as counted by count()
* The first variable in the list is assigned the first value, the second variable, the seoncd value, and so on
* @param array $keys the keys with the variable names
* @param array $values the values
foreach ($keys as $key) {
* Retrieves an assigned variable value
* @param string $name the name of the variable
* @return mixed the value of the variable
public function &get($name)
Documentation generated on Thu, 08 May 2008 16:57:49 +0300 by phpDocumentor 1.4.1