Jump to content


Photo

mergeCodeVariables()


  • Please log in to reply
1 reply to this topic

#1 Cris D.

Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts

Posted 20 February 2009 - 11:47 AM

This function is used to blend a data set (array) with presentation markup (string). It turns a plain array into a html, xhtml, xml format by inserting the data bits into the html code at "insertion points".

From thedatabase documentation.

mergeCodeVariables($content="", $rs="", $prefix="{",$suffix="}", $oddStyle="", $evenStyle="", $tag="div")

// parses any string data for template tags and populates from a resultset or single associative array
// $content = the string data to be parsed
// $rs = the resultset or associateve array which contains the data to check for possible insertion
// $prefix & $suffix = the tags start and end characters for search and replace purposes
// $oddStyle & $evenStyle = CSS info sent as style='inline styles' or class='className'
// $tag = the HTML tag to use as a container for each template object record


In simple terms, this means that if you have a single array of data (as you may get from getIntTableRows() or other queries- it can be a 'simple','array' or 'associative'=>'array'), and you want to add markup, you can create a chunk with the markup and it will be outputted once for each array in the data set.

Example:

$chunk="<h2>Topic:{topic}</h2><h3>Type:{make}</h3> <p>Detail:{detail}</p>"//any html with {insertion_points}
   
   $arr=array('topic'=>'car', 'make'=>'ford', 'engine'=>'V8');//note this an associative array of a single row of data.
   
   $output=$etomite->mergeCodeVariables($chunk, $arr, $prefix="{",$suffix="}", $oddStyle="", $evenStyle="", $tag="div");
   return $output;

will result in:



Topic: car

Type: ford

Detail: V8


This can be extended by calling an external chunk by using...
$chunk=$etomite->getChunk('chunkName');
...by using a database API to retrieve the data;
...by adding $oddStyle="class=\"odd\"", and $evenStyle="class=\"even\"" to add class names against the fields to apply css to each odd and even row.
...by adding various tags to the wrapper (ie. <p></p>, <div></div>, <tr></tr>) etc (or no wrapper at all).

Top tip: you can use a variety of characters to check for data insertion, if you use
$prefix="<!--", $suffix="-->"
then if no data exists for that field, the tags are outputted which actually marks up as html as comments and they actually dissappear from the browser!

For multi-dimentional arrays, and less overhead, you can also use parseChunk().

Edited by Cris D., 20 February 2009 - 11:57 AM.


#2 PaulD

PaulD

    Likes Etomite Forums!

  • Developers
  • PipPip
  • 413 posts

Posted 28 February 2009 - 02:36 AM

I am just starting to use these. I am going to use this to present products in different ways. This method is really cleaning up my code.

Here is a simple example that you can cut and paste and use on a test page. It is a simplified version of what I have been toying with and of course based on cris' stuff above and content from this thread from Ralph.

Snippet called 'example'. Call it into any test page with [!example!].
// Usually data for product would come from an internal 
// table using getIntTableRows or similar.
// Here I will mock up the array as if it had been returned
	$prod = array (
		'prod_id' => '45',
		'prod_name' => 'Bunch of Grapes',
		'prod_description' => 'Here is a lovely bunch of grapes',
		'prod_price' => '4.99'
		);

// assign the name of the chunk holding our display template code
$chunkName="template1";

// load the $tpl variable with chunk-based template code
$tpl = $etomite->getChunk($chunkName);

// example of using the mergeCodeVariables API function
$output .= $etomite->
  mergeCodeVariables(
	$content=$tpl,
	$prod,
	$prefix="{",
	$suffix="}",
	$oddStyle="class=\"odd\"",
	$evenStyle="class=\"even\"",
	$tag="div"
  );

return $output;

And a chunk called 'template1'
<h1>{prod_name}</h1>
<p>Prod ID: {prod_id}</p> 
<p><b>Description: </b>{prod_description}</p>
<p style="color:red;">Price: {prod_price}</p>


The snippet calls the chunk and fills the fields in curly brackets with the data from the array. How powerful is that! Amazing. I call in the data. I set a template for layout. Then that one line of code $etomite-> mergeCodeVariables() does all the work putting the two together!

By setting $chunkName (the name of the chunk containing the data template) in a page request or posted from a form, I intend to allow different presentations of the product data. Once this is up and working I can add more and more data templates over time.

I cant believe how much this technique is going to clean up my code. Hope this simple example helps someone. It is so much easier modifying layout via a chunk and data labels, rather than deep inside code, some in loops, some here, some there with long complicated php expressions. At least thats how my code usually ends up - spaghetti :-)

Paul.

Edited by PaulD, 01 March 2009 - 12:53 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users