Etomite Community Forums: parseChunk - Etomite Community Forums

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

parseChunk blends multiple array data with presentation from a chunk

#1 User is offline   Cris D. 

  • Loves Etomite Forums!
  • PipPipPipPip
  • Group: Member
  • Posts: 1,065
  • Joined: 10-August 06
  • Location:Brisbane, Queensland, Australia

Posted 28 February 2009 - 05:35 AM

parseChunk does a similar job to mergeCodeVariables, but is also allows the parsing of multiple associative arrays inside a single chunk. This function was extended for 0.6.1.4+, previously it handled only a single array.

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

Title: Hitchhikers Guide to the galaxy
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;
	}

This post has been edited by Cris D.: 03 March 2009 - 06:17 PM


#2 User is offline   Cris D. 

  • Loves Etomite Forums!
  • PipPipPipPip
  • Group: Member
  • Posts: 1,065
  • Joined: 10-August 06
  • Location:Brisbane, Queensland, Australia

Posted 20 December 2009 - 01:08 AM

One particular item that has caught me out seveeral times when using this function is that to loop through as array inside an array, it must contain an array of values, (not a single array - it MUST me an associative array).

Example: If you have $rs = array([one]=>'1', [rs2]=> array([val1]=>'1', [val2]=>'2')); and you try to pass this through parseChunk in the following manner:

{one}
{rs2} {val1} {/rs2}

This will throw an error saying "invalid argument supplied foreach". To get around this, the array you are attempting to pass needs to have a numeric value associated with it. This can be as simple as adding:

$rs2[]= array([val1]=>'1', [val2]=>'2')

Your array will now look like this:

$rs = array([one]=>'1', [rs2]=> array([0]=>array([val1]=>'1', [val2]=>'2'))) and will no longer throw foreach errors.

Another common error I make when using this function is trying to parse multiple arrays.
To do this, parseChunk needs to be looped for each array of arrays by using foreach or an if($i=0; $i<count($rs); $i++) loop.

If you don't do this, you may get a preg_match error saying:
"Compilation failed: nothing to repeat at offset 2".

This means that parseChunk can't find the first array...

Example $rs = array([0]=> [rs2]=>array([0]=>[val1]='1'), [1]=> [rs2]=>array([0]=>[val1]='1'))

Now if you try to pass the $rs array to parseChunk as $rs, then it looks at the index as the name to replace in the chunk, whereas it should be looking for the name of the array (rs2). Therefore, by including the loop, or specifying the array number, you can pass the array in a way that parsechunk can find the array name:

Will often not work for multiple arrays:
$etomite->parseChunk('chunkName', $rs, "{", "}");

Will work by specifying the array to parse or by looping through all arrays:
$etomite->parseChunk('chunkName', $rs[0] or $rs[$i], "{", "}");

#3 User is offline   Ralph 

  • Etomite Administrator
  • Icon
  • Group: Admin
  • Posts: 6,370
  • Joined: 01-July 04
  • Gender:Male
  • Location:Jamestown, NY USA
  • Interests:Computers, Camping, Hiking, Aviation

Posted 20 December 2009 - 03:39 PM

You're 100% correct, Cris... There are several shortfalls even with the modified version of parseChunk()... I do have a better version which uses conditional end-less depth recursion that is part of the new code base... It may end up in that ever elusive Prelude v1.2 as well... I have tested it extensively... Actually, I've tested the new endless-depth parser extensively and thus far it's a lot faster than the existing parser... I'll see if I can get a copy of the parseChunk() portion posted here for further testing...

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users