Jump to content


Photo

New parseChunk() Feature


  • Please log in to reply
5 replies to this topic

#1 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 29 September 2007 - 12:22 AM

Yeah, I know, we are supposed to be concentrating on bug fixes only in the Prelude code base... But I have had the need for looping in chunked templates and I was pretty sure it couldn't be all that hard to do... Well, I was right... It wasn't that hard to do and the 0.6.1.5 bug fix release will also allow loops within parsed chunks... Allow me to explain...

Let's say you have some data and want to use a chunk for display... Up until now you had several round-about ways to display a resultset in a chunk... You either had to pre-process the data and send it as a variable to the chunk, or you had to send a row chunk to mergeCodeVariables() using a loop in your snippet... Sure, 90% of what I just said makes no sense at all... But some of you get it... I'll break it down further...

Using parseChunk() goes something like this... You make an API function call something like:

// Snippet: myExample
$output = $etomite->parseChunk("myExample", array('title'=>'My Chunk', 'content'=>'The content I want displayed here.'), "{", "}");
return $output;
<!-- Chunk: myExample -->
<h1>{title}</h1>
<div>
{content}
</div>
The code above serves as a basic example of how parseChunk() works... Or at least how it has always worked up until now... The code below is an example of how the added looping feature works...

// Snippet: newExample
$rs = $etomite->getActiveChildren(0);
$output = $etomite->parseChunk("newExample", array('title'=>'New Example', 'rows'=>$rs));
return $output;
<!-- Chunk: newExample -->
<table>
<caption>{title}</caption>
{rows}
<tr>
<td>{pagetitle}</td><td>{description}</td>
</tr>
{/rows}
</table>
What you will notice is that it is now possible to send an array as a value to parseChunk() and it will be processed by looping through the array and each iteration will process the tags within a tag pair, in this case that pair being {rows}{/rows}...

Now, surely I'm not the only person that something like this excites... But, then again, you probably can't see the benefits without playing with the code... Well, I am willing to let a select few people test the modified function... PM me if you want to see how it works... Here is a link to an example on my development site...

#2 jon

jon

    Etomite Forum Newbie

  • Member
  • 39 posts

Posted 27 January 2008 - 06:28 PM

This will be a welcome new feature in the next release of Etomite. I'm used to it now, and use it extensively, but I did find the mergeCodeVariables approach more complex than I had expected when first working with Etomite a couple of months ago.

But, there is one (expected) limitation of mergeCodeVariables that I found this morning and I wonder if parseChunk overcomes: a Chunk called within another Chunk does not get populated by mergeCodeVariables.

#3 Ralph

Ralph

    Loves Etomite Forums!

  • Admin
  • 6,539 posts

Posted 28 January 2008 - 02:09 PM

This will be a welcome new feature in the next release of Etomite. I'm used to it now, and use it extensively, but I did find the mergeCodeVariables approach more complex than I had expected when first working with Etomite a couple of months ago.

But, there is one (expected) limitation of mergeCodeVariables that I found this morning and I wonder if parseChunk overcomes: a Chunk called within another Chunk does not get populated by mergeCodeVariables.


No, subsequent Chunks will not be populated as far as the use of tags within the chunks are concerned because you need to call parseChunk() in order to accomplish the task... An d I don' think you can use mergeCodeVariables() to populate nested Chunks within a single call either... You could use multiple mergeCodeVAriables() calls to accomplish this, however...

#4 jon

jon

    Etomite Forum Newbie

  • Member
  • 39 posts

Posted 28 January 2008 - 04:20 PM

I don't think you can use mergeCodeVariables() to populate nested Chunks within a single call either... You could use multiple mergeCodeVAriables() calls to accomplish this, however...

Great idea! Let me try that and report back here.

#5 jon

jon

    Etomite Forum Newbie

  • Member
  • 39 posts

Posted 28 January 2008 - 06:20 PM

I've tried various combinations of parseChunk and/or mergeCodeVariables -- the following code is the most overkill of my tests -- but always get the same results (below), without substitution of the variable in the nested Chunk:
DBTestChunk1 variable a goes here A contents and nested chunk call goes here DBTestChunk2 {b} chunk end

$rs = array("a" => "A contents", "b" => "B contents");
$output1 = $etomite->parseChunk("DBTestChunk1",$rs);
$output2 = $etomite->
mergeCodeVariables(
$content=$output1,
$rs=$rs,
$prefix="{",
$suffix="}",
$oddStyle="",
$evenStyle="",
$tag="div"
);
$output3 = $etomite->
mergeCodeVariables(
$content=$output2,
$rs=$rs,
$prefix="{",
$suffix="}",
$oddStyle="",
$evenStyle="",
$tag="div"
);
return $output3;

DBTestChunk1:
<div>DBTestChunk1 variable a goes here {a} and nested chunk call goes here {{DBTestChunk2}} end</div>

DBTestChunk2:
DBTestChunk2 {b} chunk

Perhaps you could elaborate on anything you think might work. My only remaining idea is to scan the chunk looking for {{ and }}, and do my own parsing for chunks.

#6 Cris D.

Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts

Posted 29 January 2008 - 07:41 PM

I tried many thing while trying to get the RSS feed snippet to parse nested code. The best success I got way by nesting snippets and calling the top snippet call(s) cached. Therefore you could try doing what you have above, but make each nest it's own snippet and call it with the runSnippet() API.

[[snippet1]]
$rs = array("a" => "A contents", "b" => "B contents");
$output1 = $etomite->parseChunk("DBTestChunk1",$rs);
$output1 .=$etomite->runSnippet('snippet2');
return $output;

[[snippet2]]
$rs = array("a" => "A contents", "b" => "B contents");
$output2 = $etomite-> 
  mergeCodeVariables( 
	$content=$output1, 
	$rs=$rs, 
	$prefix="{", 
	$suffix="}", 
	$oddStyle="", 
	$evenStyle="", 
	$tag="div" 
  ); 
$output2 .=$etomite->runSnippet('snippet3');
return $output;

[[snippet3]]
$rs = array("a" => "A contents", "b" => "B contents");
$output3 = $etomite-> 
  mergeCodeVariables( 
	$content=$output2, 
	$rs=$rs, 
	$prefix="{", 
	$suffix="}", 
	$oddStyle="", 
	$evenStyle="", 
	$tag="div" 
  ); 
return $output3;

Of course you could abstract this out further to get the data from a snippet also by using...
[[dataSnippet]]
$rs = array("a" => "A contents", "b" => "B contents");
return $rs;

and calling the data in with
$rs=$etomite->runSnippet('dataSnippet');

The default snippet runs in the index.php file I think is '5' but it can also be tweaked a bit (at the end of the file).

I know this looks fiddley but it would probably be easier than building your own parser.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users