Jump to content


parseChunk


  • You cannot reply to this topic
5 replies to this topic

#1 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

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;
	}

Edited by Ralph, 15 September 2011 - 01:23 PM.
Fixed example codes tag pair closing tag backslash to a slash


#2 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

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 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,506 posts
  • Gender:Male

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...

#4 psusu

    Etomite Forum Newbie

  • Member
  • 2 posts

Posted 15 September 2011 - 10:20 AM

Hi Cris

this is my first post and I'm trying to learn how this function works. Can you help me?

I've followed your example and I made a Snippet like this:
$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('prova',$arr, '{','}');
return $output;

the chunk is called prova:
<h2>Title:{title}</h2>
  <p>Description:{description}</p>
  <h3>Comments:</h3>
  {rs1}
  <p>Comment by:{author} member since:{year} {comment}</p>
  {\rs1}

this setting returns:


Title:Hitchhikers Guide to the galaxy

Description:A Cool book about funky stuff in space.

Comments:

{rs1}

Comment by:{author} member since:{year} {comment}

{\rs1}


where I'm wrong?
Thank you

#5 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,506 posts
  • Gender:Male

Posted 15 September 2011 - 12:43 PM

See http://dahlgren.slyi...ParseChunk.html and http://ralphdahlgren...utorial105.html for additional information if you haven't already done so... The problem is the layout of your data loop tag pair section... It should be more like the code below... You had a backslash instead of a slash in the tag pair closing tag...

<h2>Title:{title}</h2>
<p>Description:{description}</p>
<h3>Comments:</h3>
{rs1}
<p>Comment by:{author} member since:{year} {comment}</p>
{/rs1}

The code in the original example provided by CrisD has also been corrected...

#6 psusu

    Etomite Forum Newbie

  • Member
  • 2 posts

Posted 27 September 2011 - 08:44 AM

Thank you Ralph!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users