Re: Dynamic blocks?

From: Date: Thu, 31 May 2001 21:13:13 +0000
Subject: Re: Dynamic blocks?
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-848@lists.php.net to get a copy of this message
"Ray A. Akey" wrote: > > Does Smarty have dynamic blocks like fasttemplate has? I don't want to > capture all my database records into a big array and then use a section > loop. Can I ask why? Smarty allows you to loop through array data in the template so you don't _have_ to do this catenation of data at the application level. With Smarty, in your application you append the data to an array assigned to the template. Then in the template you loop them with a section, having individual control over the display of each element. If you _really_ want to handle things in the manner of the way FastTemplates does, you can catenate your data together in the application by calling your "dynamic block" template over and over, then assign this "block" to another template variable for display. Of course the dynamic block won't be imbedded in the template, so it's not exact. But once you put Smarty's method of looping data to the test, you should be able to see the benefits of this method and never look back. Now realize with Smarty, you gain the power of the template language at your fingertips within the section loop, and the code in the application is minimized to 1) collect data 2) assign data 3) call template. Remember, we don't want anything to do with presentation in the application, that should all be done in the template! Of course my opinion is bias being an author of Smarty, so others with experience with both template solutions, please chime in your thoughts. Monte > > Is there some way to just sit inside a database result loop and echo the > results row-by-row? An example would be very helpful. > FastTemplate-ish way, using Smarty: // accumulate the row data into a variable, like FastTemplate while($row->next) { $smarty->assign("name",$row->name); $row_accumulation .= $smarty->fetch("row.tpl"); } $smarty->assign("rows",$row_accumulation); $smarty->display("index.tpl"); row.tpl ------- <TR><TD>{$name}</TD></TR> index.tpl --------- <TABLE> {$rows} {/TABLE> Or, the Smarty way: // application code is very simple, nothing to do with presentation here $smarty->assign("name",$name_array); $smarty->display("index.tpl"); index.tpl --------- <TABLE> {section name=rows loop=$name} {* full control of presentation, even per row *} <TR><TD>{$name[row]}</TD></TR> {/section} </TABLE> Monte

« previous php.smarty.general (#848) next »