CHUNK EXAMPLE:
<h2>Title: {title}</h2>
<p>Description:{description}</p>
<h3>Comments:</h3>
{rs1}
<p>Comment by:{author} member since:{year} {comment}</p>
{/rs1}
Note above that {author}{year}and {comment} are wrapped in another tag: {rs1}{/rs1}parseChunk will actually recognise this wrapper tag and loop through the results of the array called 'rs1' and substitute the placeholders in that array for that part of the chunk.
The effect is like having the ability to send mulitple mergeCodeVariables() but from from a single function and a single chunk.
Example of a snippet use:
$arr=array();
$arr['title']='Hitchhikers Guide to the galaxy';
$arr['description']='A Cool book about funky stuff in space.';
$arr['rs1']=array(
array('author'=>'Mr.X','year'=>'2001','comment'=>'Too far out for me.'),
array('author'=>'Mrs. XYZ','year'=>'2006','comment'=>'I loved it!'),
array('author'=>'Billy','year'=>'2004','comment'=>'What a wack job.')
);
$output = $etomite->parseChunk('myChunk',$arr, '{','}');
This would output:Quote
Description:A Cool book about funky stuff in space.
Comments
Comment by:Mr.X member since: 2001 Too far out for me.
Comment by:Mrs. XYZ member since: 2006 I loved it!
Comment by:Billy member since: 2004 What a wack job.
See how you can tell parseChunk to stop and play in a certain area of your chunk. This saves time, the number of chunks you use, the number of snippets you use and the number of lines of code you have to write and maintain.
Note also, that parseChunk will actually get the chunk for you, this is different from mergeCodevariables() where you have to get the chunk first by using getChunk().
The arrays above were manually built, but could just have easily been an array returned from any of the relevant etomite functions like getIntTableRows() etc. You can send as many additional arrays as you like if you add the associated wrapper in the chunk.
All in all, parseChunk is a very flexible, smart and efficient function.
From the index.php file:
function parseChunk($chunkName, $chunkArr, $prefix="{", $suffix="}") {
// returns chunk code with marker tags replaced with $key=>$value values
// $chunkName = the textual name of the chunk to be parsed
// $chunkArr = a single dimensional $key=>$value array of tags and values
// $prefix and $suffix = tag begin and end markers which can be customized when called
// Modified 2007-09-28 by Ralph to allow $key=>array($keys=>$values) to be
// sent which will be processed by looping through code wrapped within {tag}{/tag} pairs.
// Example: {tag}<tr><td>{col1}</td><td>{col2}</td></tr>{/tag}
if(!is_array($chunkArr) || count($chunkArr) < 1) {
return false;
}
$chunk = $this->getChunk($chunkName);
foreach($chunkArr as $key => $value)
{
if(!is_array($value))
{
$chunk = str_replace($prefix.$key.$suffix, $value, $chunk);
}
else
{
if(preg_match("|".$prefix.$key.$suffix."(.+)".$prefix.'/'.$key.$suffix."|s", $chunk, $match)
&& count($value) > 0)
{
$loopData = '';
foreach($value as $row)
{
$loopTemp = $match['1'];
foreach($row as $loopKey => $loopValue)
{
$loopTemp = str_replace($prefix.$loopKey.$suffix, $loopValue, $loopTemp);
}
$loopData .= $loopTemp;
}
$chunk = str_replace($match['0'], $loopData, $chunk);
}
}
}
return $chunk;
}
Edited by Ralph, 15 September 2011 - 01:23 PM.
Fixed example codes tag pair closing tag backslash to a slash











