Re: [SMARTY] Seperation of Logic and Presentation
| From: | Monte Ohrt | Date: | Fri, 29 Jul 2005 19:02:54 +0000 |
| Subject: | Re: [SMARTY] Seperation of Logic and Presentation | ||
| References: | 1 2 3 4 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-12900@lists.php.net to get a copy of this message | ||
OK while we're sharing, here is my setup (or rather some of its basics)
We have an environment with multiple sites (clients) that can have multiple "themes", or look-and-feel to their site that they can swap with the change of one variable. Then there are "modules" such as news, calendar, classifieds, etc. The top-level client/site directory structure is as follows (these are not under doc root!):
/templates/
/theme1/
init.tpl
header.tpl
footer.tpl
layout.tpl
/mod1/
location_top.tpl
location_left.tpl
location_main_content.tpl
/images/
/theme1/
masthead.gif
/mod1/
module.gif
/css/
/theme/
/mod1/
/configs/
/theme/
lang_en.conf
lang_de.conf
/mod1/
templates, images, css and config files are broken down to global, theme-specific and module-specific directories. We keep language-specific config files under each theme.
I have one "controller" PHP script that acts as the controller (see the guestbook example app for complete example)
This script will do something like this (crude example):
index.php:
$smarty = new Smarty;
// set from config file
$smarty->theme = 'bluestuff';
// set by running module
$smarty->mod = 'calendar';
// do some stuff to figure out what page we are displaying
// do whatever necessary tpl var assignments here
$smarty->assign('page','main_menu');
$smarty->display("init.tpl");
The init.tpl acts as the template controller, it basically loads the global init.conf then loads the theme-specific init.tpl:
init.tpl:
{config_load file="init.conf"}
{include file="$theme/init.tpl"}
$theme/init.tpl then loads theme-specific stuff, then decides if we're loading the main layout or something else:
{config_load file="$theme/init.conf"}
{config_load file="$theme/lang_$lang.conf}
{if $page eq "showimage"}
{include file="$theme/showimage.tpl"}
{elseif $page eq "printable"}
{include file="$theme/printable.tpl"}
{else}
{include file="$theme/layout.tpl"}
{/if}
The $theme is set in the constructor for the current "theme". There is an init.conf that contains global config vars, and also an init.conf for each theme for theme-specific settings. layout.tpl contains the framework for each page, it minimally contains location cells that can be controlled per module. Notice that the layout can be skipped, such as an "image-only" or "printable" page. Here is a watered-down example of a layout.tpl:
layout.tpl:
{include file="$theme/header.tpl"}
<table>
<tr>
<td colspan="2">{include file="$theme/$mod/location_top.tpl"}</td>
</tr>
<tr>
<td>{include file="$theme/$mod/location_left.tpl"}</td>
<td>{include file="$theme/$mod/location_main_content.tpl"}</td>
</tr>
</table>
{include file="$theme/location_footer.tpl"}
That is a very crude example, but basically you put your HTML/CSS here to make the theme look how you want. Each module can control what shows up in the location templates. location tpls are useful for banners, module navigation, etc. The location_main_content.tpl is what shows up in the center of the page, it minimally includes module-specific content and templates for whatever page we are on, such as:
{include file="$theme/$mod/$page.tpl"}
Then you have a template for each module page in that directory.
That should be enough to put everyone to sleep :)
Monte