|
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 |
foxmask Smarty Rookie
Joined: 04 Feb 2004 Posts: 23 Location: Paris / France
|
Posted: Wed Feb 04, 2004 9:07 am Post subject: Cascading Call of Templates ? |
|
|
Hi everyone,
before Smarty i used Template.inc from PHPLib ; 2 years ago ; and i built page like that
Code: |
<html>
<head><title>foo</title></head>
<body>
{$left_menu}
{$center}
{$right_menu}
</body>
</html>
|
left menu looked like that
Code: |
<div>
<a href="link1.html">Link1</a><br/>
<a href="link2.html">Link3</a><br/>
<a href="link3.html">Link3</a><br/>
</div>
|
with this main template, in my php script i made call of the others peace of my main page.
The question is how can i do to include each peace of page in the main template ? _________________ Smarty Fan Since 2.5 |
|
Back to top |
|
foxmask Smarty Rookie
Joined: 04 Feb 2004 Posts: 23 Location: Paris / France
|
Posted: Wed Feb 04, 2004 4:42 pm Post subject: More details |
|
|
my index.tpl
Code: |
{* Entete *}
{include file="header.tpl" titre_page="News"}
{* Templates du Centre *}
{$data}
{* Pied de Page *}
{include file="footer.tpl"}
|
news.tpl template
Code: |
<div>
<p>
<span>{$titre_news}</span><br/>
<span>le {$date_news} par {$signature_news}</span><br/>
<span>{$contenu_news}</span><br/>
</p>
|
and finnally index.php script :
Code: |
equire ('setup.php');
$smarty = new Smarty_album;
$smarty->caching = true;
// efface tous les fichiers du cache
$smarty->clear_all_cache();
// efface le fichier de cache du template 'index.tpl'
$smarty->clear_cache('index.tpl');
$db = new DB_Album();
$db->query('SELECT * FROM al_news');
while($db->next_record()) {
$smarty->assign('titre_news', $db->f('titre'));
$smarty->assign('date_news', $db->f('date'));
$smarty->assign('signature_news', $db->f('signature'));
$smarty->assign('contenu_news', $db->f('contenu'));
$o = $smarty->fetch('news.tpl');
$smarty->assign('data', $o);
}
$smarty->display('index.tpl');
|
the output only give me ONE record of my al_news table
can someone explain me whats wrong ?
i'm a beginner with using Smarty _________________ Smarty Fan Since 2.5 |
|
Back to top |
|
zbert Smarty Rookie
Joined: 11 Dec 2003 Posts: 30 Location: New York
|
Posted: Thu Feb 05, 2004 6:54 am Post subject: |
|
|
Look in the manual under "section,sectionelse". You need to loop through the results of your query and write out to html on each pass.
Code: | <!-- Iterating through the array -->
{section name=i loop=$project_id}
<tr>
<td>{$project[i]}</td>
<td>{$description[i]}</td>
</tr>
{/section}
|
--zbert |
|
Back to top |
|
foxmask Smarty Rookie
Joined: 04 Feb 2004 Posts: 23 Location: Paris / France
|
Posted: Thu Feb 05, 2004 12:20 pm Post subject: its now ok |
|
|
i did :
i put my data in an array and then read this one with a foreach as follow :
index.php
Code: |
<?php
$smarty = new Smarty_album;
$smarty->caching = true;
// efface le fichier de cache du template 'index.tpl'
$smarty->clear_cache('main.tpl');
$data=array();
while($db->next_record()) {
$date = $db->f('date');
list($an,$mois,$jour)=split('-',$date);
$new_date = "$jour-$mois-$an";
$data[] = array('titre_news' => $db->f('titre'),
'date_news' => $new_date,
'signature_news' => $db->f('signature'),
'contenu_news' => $db->f('contenu'),
'id_news' => $db->f('id_news'));
}
$smarty->assign('tpl_file', 'news_sub.tpl');
$smarty->assign('data', $data);
$smarty->display('main.tpl');
?>
|
news_sub.tpl (the sub template)
Code: |
<div>
<p>
<span>{$center_item.titre_news}</span><br/>
<span>le {$center_item.date_news} par {$center_item.signature_news}</span><br/>
<span>{$center_item.contenu_news}</span><br/>
</p>
{if $next_page > $nb_news/#NEWS_PAR_PAGE# and $nb_news > 0 }
<a href="/index.php?page={$prev_page}">&& Page Précédente</a> --
{/if}
{if $nb_news > $next_page }
<a href="/index.php?page={$next_page}">Page Suivante &&</a><br/>
{/if}
</div>
|
main.tpl (the main render for all the website)
Code: |
{config_load file="config.ini"}
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{#TITRE_DU_SITE#}: {$titre_page}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
{* Templates de 'Navigation' *}
{include file="nav.tpl"}
{* Templates du Centre *}
{foreach from=$data item=center_item}
{include file="$tpl_file"}
{/foreach}
</body>
</html>
|
Thanks to messju for his help.
_________________ Smarty Fan Since 2.5 |
|
Back to top |
|
zbert Smarty Rookie
Joined: 11 Dec 2003 Posts: 30 Location: New York
|
Posted: Fri Feb 06, 2004 1:03 am Post subject: |
|
|
Quote: | {* Templates du Centre *}
{foreach from=$data item=center_item}
{include file="$tpl_file"}
{/foreach} |
I'm not sure I know what the include file is there for. Have you tried:
Code: | {* Templates du Centre *}
{foreach from=$data item=center_item}
My data is: {$data}<br>
{/foreach} |
--zbert |
|
Back to top |
|
foxmask Smarty Rookie
Joined: 04 Feb 2004 Posts: 23 Location: Paris / France
|
Posted: Fri Feb 06, 2004 8:39 am Post subject: |
|
|
That display "my data is Array" on each line of course.
My previous post work fine like that.
My Goal was to build a main template in which i include everything else i need.
Thus i dont need to to
Code: |
{include file="header.tpl"}
......body.......
{include file="footer.tpl"}
|
in every other templates.
As all my header / footer are the same (except for the title of each page i manage easyly)
my main.tpl is enough.
And when i have just one thing to display in the center i made a dummy array $data just to be able to include the template i need
example :
Code: |
$data=array("bla"=>"bla");
$smarty->assign('tpl_file', 'admin_index_sub.tpl');
$smarty->assign('data',$data);
$smarty->display('admin_main.tpl');
|
and like that i can include admin_index_sub.tpl which follows :
Code: |
<div>
<span>Administration Manager</span>
</div>
|
that's all... _________________ Smarty Fan Since 2.5 |
|
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
|