Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Creating a Template System    Marked Cool    (Review this resource)
Author: econner
Posted On: 2004-11-15
Category: Home > PHP Tutorials

Popularity: 2 points out of 10    

Description: Create a template system using Smarty

Total Hits: 3114     Total Votes: 8     Total Points: 31 (9 reviews)        [ Download ]   

Page Navigation:  [1]


Creating a Template System


This tutorial will demonstrate how to set up and use smarty and explore some of the functions it offers.

First of all, what is the importance of templates? I mean I can just print everything out right?
While it is easy to print information out, it can hinder the readibility, reusability, and functionality of your code. Seperating display from functionality can be a BIG help especially when creating large applications. You don't have to think about coding display into your functions and don't get caught up in hundreds of echo statements. Templates are also quite a bit easier and more professional for most tasks once you get the hang of them.

The first thing you'll need to do is download the latest release of smarty available at http://smarty.php.net.

Once you've downloaded smarty, unzip it and upload the libs file in the smarty directory to your webserver and rename it smarty. This contains all the smarty libraries and functionality, it is vital to your code.

Next, create a new directory and call it "templates," this will contain all of the html for your application. Within this directory create two new directories call the first one "compile" and the second "html". CHMOD the compile folder to 777 if using linux, smarty accesses it. After this, create a new php file named libs.inc.php in your templates folder similar to the one below.

 

<?php

# Filename: libs.inc.php *it's important to name your files like this so you can keep them straight*

# the exact path to this file
$path = dirname(__FILE__);

# change this value to the directory where you uploaded the smarty libs
define ("SMARTY_DIR", "/home/smarty/");

require_once (SMARTY_DIR."Smarty.class.php");
$smarty = new Smarty;
$smarty->compile_dir = "$path/compile";
$smarty->template_dir = "$path/html";

?>

Ok smarty is now usable with your files.
To be able to display templates you'll need to include this line on all of your files:
require_once ("./templates/libs.inc.php");

To display a smarty file, use the following code:
$smarty->display("myfile.html");
Thats it, you've set up your template system. Now I'll go into more detail about some of the functions smarty offers.

Variables
To assign a variable for smarty to read:
$smarty->assign("message", "Hello");
The first parameter is the name the variable will be when passed to smarty, the second is the value of that variable.
To call a variable from a smarty file you use {$variablename} so our message variable would be {$message}.

Loops/Arrays
In a file, create an array and assign it to smarty:
$names = array("john", "joe", "eric", "matt");
$smarty->assign("names", $names);

Now to loop through it and print it with smarty:
{section name=i loop=$names}

        Name: {$names[i]}<br>
{/section}

A two dimensional associative array:
$smarty->assign('names', array(
array('name' => 'john', 'city' => 'Golden'),
array('name' => 'joe', 'city' => 'Bakersville'),
array('name' => 'eric', 'city' => 'Pleasantville'),
array('name' => 'matt', 'city' => 'TownCity'),
));
Displaying with smarty:
{section name=i loop=$names}
        Name: {$names[i].name}<br>
        City: {$names[i].city}<br>
{/section}


To start a loop at a different value (default is zero) use start:

{section name=i loop=$names start=1} // will start looping at the value 1 rather than zero
To step through an array at a different value:
{section name=i loop=$names step=2} // i will increment by 2's


Cycling values:
{section name=i loop=$names}
       <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
              <td>{$users[i].name}</td>
       </tr>
{/section}
This will cycle the background color for each row in the table

Predefined Variables
{* display value of page from URL (GET) http://www.domain.com/index.php?page=blah *}
{$smarty.get.page}

{* display the variable "page" from a form (POST) *}
{$smarty.post.page}

{* display the value of the cookie "username" *}
{$smarty.cookies.username}

{* display the server variable "SERVER_NAME" *}
{$smarty.server.SERVER_NAME}

A few more useful:
{$smarty.server.PHP_SELF} // will give the name of the current php app (useful in forms)
{$smarty.section.i.iteration} //will return the current loop iteration of the section named i
{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} //will return the current date formatted


There is quite a bit more available in the smarty manual, it can do a lot!



Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.