|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
brettz9 Smarty Regular
Joined: 07 Jul 2006 Posts: 93
|
Posted: Thu Jul 20, 2006 3:30 am Post subject: Approach for formatting results (tip or dud?) |
|
|
Hi,
In order to deal with display of complex structures (e.g., hierarchies or tables with dynamic contents) while avoiding the overhead (and extra work) of going through an iteration to build the structure in regular PHP (using formatting variables set in the template) and display the structure in Smarty, I was interested in any feedback on the following approach.
I didn't think that a custom function/template would be possible or justified if one wished to allow for higher complexity and use the results frequently.
I think the following can still allow templaters to get a fair view of and customize the final ouput (via the variables), while the main PHP script handles the iteration, etc. (one interesting use, I think, might be Xqueries, as these tend to mix formatting and data (currently only possible in PHP it seems through Berkeley DB XML), etc.), though this could also work with just a simple build of a dynamic table or XML result.
Template examples:
table.tpl
Code: |
{* Assign variables to be used in final display *}
{assign var=row value="<tr>"}
{assign var=rowend value="</tr>"}
{capture name=cell}
<td>Test:
{/capture}
{assign var=cellend value="</td>"}
{* Display the results as returned from the script (using the variables above) *}
<table>
{displayresults}
</table>
|
list.tpl
Code: |
{* Assign variables to be used in final display *}
{assign var=row value="<li>"}
{assign var=rowend value="</li>"}
{capture name=cell}
<li>Test:
{/capture}
{assign var=cellend value="</li>"}
{* Display the results as returned from the script (using the variables above) *}
<ul>
{displayresults}
</ul>
|
PHP script:
[php:1:a175e21f56]
$dead = $smarty->fetch('index.tpl'); // Get the as-yet uninstantiated template with its own variables
$tpl_vars = $smarty->get_template_vars(); // Put the template variables into an array
// Add code here which accesses $tpl_vars mixed with obtaining results from a data source to populate a complex structure (here, $displayresults) with the template's variables
$smarty->assign('displayresults', $displayresults);
if ($some_condition) $smarty->display('table.tpl');
elseif ($another_condition) $smarty->display('list.tpl');
[/php:1:a175e21f56]
Although using get_template_vars would add the overhead of going through the whole template, as it was not populated yet in the beginning, I am guessing execution shouldn't be so long (especially if compared against building a large result set in both PHP code and Smarty) |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|