|
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 |
Jetshu Smarty n00b
Joined: 15 Mar 2004 Posts: 2
|
Posted: Mon Mar 15, 2004 4:31 am Post subject: Smarty recursion through an array |
|
|
I want to use smarty to display information i have in an array as part of my site and i want to know if i can use the $smarty->fetch method to do this...here's my plan...
Code: | $output = '';
while($i < 5)
{
$smarty->assign('title','headings[$i]';
$smarty->assign('content','content[$i]';
$output = $output . $smarty->fetch('format.tpl');
$i++;
}
$smarty->assign('output',$output);
$smarty->display('list.tpl'); |
so say format.tpl is a table with a td for the heading and a td for the content...would something of this sort pull the data and store whatever number of tables in output and then display that in list.tpl which woudl be the complete list of tables and data. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Mar 15, 2004 8:33 am Post subject: Re: Smarty recursion through an array |
|
|
When it comes to stitching parts together (which I see as a design issue), I try to pass data to templates that will do the stitching for me and leave it out of the application code. Maybe do this in-template?
Something like:
[php:1:aa5c40e73e]$smarty->assign('headings', $headings);
$smarty->assign('content', $content);
$smarty->display('list.tpl');[/php:1:aa5c40e73e]
Code: | <table>
{section name=i loop=$headings}
<tr>
{include file="format.tpl" title=$headings[i] content=$content[i]}
</tr>
{/section}
</table> |
|
|
Back to top |
|
brianman Smarty n00b
Joined: 23 Jul 2004 Posts: 1 Location: Sveg, Sweden
|
Posted: Mon Aug 02, 2004 6:42 am Post subject: |
|
|
This is how I do it at the moment, not sure if this is the right way, if not please enlighten me of better ways to do it :)
news.php
[php:1:3be4e76ef3]<?php
/**
* Fetch news/blogs.
*/
$sql = "SELECT * FROM news ORDER BY id DESC";
$items =& $db->getAll($sql);
if (DB::isError($res)) {
exit('<pre>'.$res->getMessage()."\n".$res->getDebugInfo().'</pre>');
}
require_once('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('items', $items);
$smarty->display('news.tpl');
?>[/php:1:3be4e76ef3]
news.tpl
Code: |
{**
* Loop through the items/posts.
*}
{section name=items loop=$items}
{**
* Capture the title for the item/post "box".
*}
{capture name="postTitle"}
<span class="postDate">{$items[items].time|date_format:"%Y-%m-%d"}</span> <span class="postTime">{$items[items].time|date_format:"%H:%M:%S"}</span>
{/capture}
{**
* Capture the text for the item/post "box".
*}
{capture name="postText"}
{strip}{$items[items].text|nl2br}{/strip}
{/capture}
{**
* Include (create) the item/post "box" and assign the captured elements.
*}
{include file="template.post.tpl"}
{/section}
|
template.post.tpl
Code: |
<div class="post">
<p class="postTitle">{strip}{$smarty.capture.postTitle}{/strip}</p>
<p class="postText">
{strip}{$smarty.capture.postText}{/strip}
</p>
</div>
|
|
|
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
|