Panwebtek Professional web Development

Professional Web Development

Three-Tier Architecture, Web Design and Smarty

What is Three-Tier Architecture?

Most of simple web sites are designed by just using HTML, some Javascript and some CSS. However when web sites need to access databases and modify data entered on the client side or coming from the server, a three-tier architecture is recommended. This type of architecture will allow the designer to split each piece of the application's functionality into separate components based on what they do, and group each kind of component into a single logical tier. So what are these tiers? We usually split each type of component in:

The presentation tier

The business tier

The data tier

Let's quickly review what each layer does:

The presentation tier contains the user interface components of the site and allows the user to interact with the business layer.
The business tier receives requests from the presentation layer and return a result to the presentation layer. In order to do that, the business tier needs to access the data tier and extract the raw data, and transform it according to the presentation's request.
The data tier stores the data (usually in a database) that will be served to the business tier when requested.

What is Smarty?

Smarty is a template engine for PHP, and allows to separate application logic and content from its presentation. Using Smarty, a programmer can develop the logic of the application without being concerned with the layout, and the designer can build the templates without being concerned with logic, since he will not be on contact with the logic code; therefore, template designers will not mess up the logic code and application developers will not mess up the templates. Now that we have an idea of why we use Smarty, let's see how Smarty can make a programmer's life more bearable.

Let's see some examples of how we use PHP and Smarty in common tasks like looping:

Loops with {section}

We assign an array to Smarty:

<?php
   $id = array(100,101,102);
   $smarty->assign('agentid',$id);
?>

The template that outputs the array will have the following code:

{* print out all the values of the $agentid array *}
   {section name=agent loop=$agentid}    
   id: {$agentid[agent]}
   {/section}

The output will be:
id: 100
id: 101
id: 102

If an array is not assigned, we will have:

{section name=evenumbers start=10 loop=20 step=2}    {$smarty.section.evenumbers.index}
{/section}

The output will be:
10 12 14 16 18

Configuration Files Configuration files are files used to store the value of variables to be used later by Smarty template designers. These variable are treated as global variables and can contain site configuration information, the site title, the footer and the copyright information for example.

Configuration files can store information under sections or without sections. Let's look at the following configuration file.

myconfiguration.conf

Title = “MySite” SloganText = “Think Smart!”
[NavigationPanel]
ThColor = #cf43ff
[Footer]
CopyrightText = “All rights reserved by PanWebTek”;

The template designers have access to this files and to the global variables by using the function:{config_load}.
In the template file, you use {config_load}:

template.tpl

{config_load file=myconfiguration.conf} {#Title#}

So, {#VariableName#} can be used in the template after loading the configuration file. To use the section footer:

footer.tpl

{config_load file=myconfiguration.conf section= "Footer"} {#CopyrightText#}

Smarty is a very smart template and can help you save a lot of time in your coding as well as separate page design from logic processing. We will have more on the subject!



Copyright 2008 - PanWebTek LLC./ www.MyHostingPalace.com