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

How I work with Smarty: Arrays filled with the content.

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
peroSine
Smarty Rookie


Joined: 15 Feb 2006
Posts: 8

PostPosted: Sat Feb 18, 2006 12:18 pm    Post subject: How I work with Smarty: Arrays filled with the content. Reply with quote

Thought I would share how I work with smarty. Before outputting the entire page with display() I first define the contents of the site in an array with the following:

[php:1:76661e22b8]<?
// Smarty (smarty.php.net)
require_once('library/smarty/Smarty.class.php');
$template = new Smarty();

// Configuration
require_once('include/config.inc.php');

// Initiate template arrays
$aCSS = array();
$aContent = array();
$aSidebar = array();

// Page navigation
// check if page is requested
if(isset($_REQUEST['page']) && !(trim($_REQUEST['page'] == ""))) $sPage = $_REQUEST['page'];
else $sPage = "";

switch($sPage)
{
// HOMEPAGE
default;
// do whatever you want here to fill up the Content array with fetch.
$aContent[] = template->fetch('introduction.tpl')
// required stylesheet for introduciont.tpl
$aCSS[] = 'introduction';
$aContent[] = template->fetch('newsLatest.tpl');
break;

// NEWS
case 'news';
// same here
break;

// Output the used CSS + Sidebar + Content
$template->assign('aCSS', $aCSS);
$template->assign('aContent', $aContent);
$template->assign('aSidebar', $aSidebar);

// Print the template
$template->display("index.tpl");
?>[/php:1:76661e22b8]

Normally I work with different classes/functions in the above 'cases' which fill up the content and sidebar arrays for me with the correct templates.

Than in the index.tpl I have the following:
Code:

// Header abobe
{* Stylesheets *}
{foreach from=$aCSS item=css}
<link rel="stylesheet" href="{$TEMPLATEDIR}/stylesheets/{$css}.css" type="text/css" media="all" />
{/foreach}

</head>
<body>
<div id="container">
   {* Header *}
   <div id="contentWrap">
      {if $aSidebar}
      <div id="sidebar">
         {foreach from=$aSidebar item=sidebar}
            {$sidebar}
         {/foreach}
      </div>
      {/if}
      <div id="content">
         {foreach from=$aContent item=content}
            {$content}
         {/foreach}
      </div>   
   </div>
   <br class="clearer" />
   {* Footer *}
</div>
</body>


What's the benefit of this? I first fill up the needed content and sidebar without ouputting anything. This way there's always room voor modification or header(Location....).

I don't know if this is a normal way of working with smarty or if it's completly wrong, or anything in between. I'm just interested in your opionion.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Mar 06, 2006 7:25 pm    Post subject: Reply with quote

Hello... I am doing something similar, except that I do not actually fetch the templates... Instead I have 3 arrays - left, middle & right, which hold the names/paths of the templates that will be used to generate the final page...

Then I call my "master" template which is again almost exactly the same as your... instead of output the variable {$content} mine is more like

Code:

{foreach item=centeritem from=`$ContentArray.center`}
   <div class="contenttable" style="clear:both;">
      {include file=$centeritem}
   </div>
{/foreach}


I think in the end we are basically doing the same thing.
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
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 -> Tips and Tricks 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