Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Cascading Call of Templates ?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
foxmask
Smarty Rookie


Joined: 04 Feb 2004
Posts: 23
Location: Paris / France

PostPosted: Wed Feb 04, 2004 9:07 am    Post subject: Cascading Call of Templates ? Reply with quote

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
View user's profile Send private message Visit poster's website
foxmask
Smarty Rookie


Joined: 04 Feb 2004
Posts: 23
Location: Paris / France

PostPosted: Wed Feb 04, 2004 4:42 pm    Post subject: More details Reply with quote

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 Wink
_________________
Smarty Fan Since 2.5
Back to top
View user's profile Send private message Visit poster's website
zbert
Smarty Rookie


Joined: 11 Dec 2003
Posts: 30
Location: New York

PostPosted: Thu Feb 05, 2004 6:54 am    Post subject: Reply with quote

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
View user's profile Send private message
foxmask
Smarty Rookie


Joined: 04 Feb 2004
Posts: 23
Location: Paris / France

PostPosted: Thu Feb 05, 2004 12:20 pm    Post subject: its now ok Reply with quote

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.
Wink
_________________
Smarty Fan Since 2.5
Back to top
View user's profile Send private message Visit poster's website
zbert
Smarty Rookie


Joined: 11 Dec 2003
Posts: 30
Location: New York

PostPosted: Fri Feb 06, 2004 1:03 am    Post subject: Reply with quote

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
View user's profile Send private message
foxmask
Smarty Rookie


Joined: 04 Feb 2004
Posts: 23
Location: Paris / France

PostPosted: Fri Feb 06, 2004 8:39 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP