Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.
1. Download:
You can download Smarty from homepage: http://www.smarty.net/download
2. Installation
Install the Smarty library files which are in the /libs/ sub directory of the distribution. These are .php files that you SHOULD NOT edit. They are shared among all applications and only get changed when you upgrade to a new version of Smarty.
In the examples below the Smarty tarball has been unpacked to: /usr/local/lib/Smarty-v.e.r/ for *nix machines and c:\your_web_root\libs\Smarty-v.e.r\ for the windows environment.
3. Using:
First, require Smarty class you can require directly or define a global value for example index.php:
<?php require_once('config.php'); require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty(); // Header $smarty->assign('page_title','Demo Smarty'); // Body $smarty->assign('body','Body'); // Footer $smarty->assign('page_footer','Copyright by www.9tuts.net'); // Display $smarty->display('home.tpl'); ?>Smarty requires four directories which are by default named templates/, templates_c/, configs/ and cache/ in your root website folder
Create a file into templates folder: home.tpl
{* Comment Not Display *} <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>{$page_title}</title> </head> <body> {$body} <div>{$page_footer}</div> </body> </html>
More examples you can find at demo folder in download packet.