Smarty For Smart Developers

by Hiroshi on October 27, 2008

in CMS, Content Management, Softwares

SMARTY – What & Why

SMARTY is a template engine for PHP. More specifically, it facilitates a manageable way to separate application logic and content from its presentation. This is best described in a situation where the application programmer and the template designer play different roles, or in most cases are not the same person.

Some of Smarty’s features

  • Smarty is extremely fast.
  • It is efficient since the PHP parser does the dirty work.
  • No template parsing overhead, only compiles once.
  • It is smart about recompiling only the template files that have changed.
  • You can easily create your own custom functions and variable modifiers, so the template language is extremely extensible.
  • Configurable tag syntax.
  • The if elseif constructs are passed to the PHP parser, so the if expression syntax can be as simple or as complex an evaluation as you like.
  • Allows unlimited nesting of sections, if’s etc.
  • It is possible to embed PHP code right in your template files, although this may not be needed (nor recommended) since the engine is so customizable.
  • Built-in caching support. (wow. No more low resources server burden).
  • Arbitrary template sources.
  • Custom cache handling functions.
  • Plugin architecture.
  • Re usability of code.

Smarty requires a web server running PHP 4.0.6 or greater.

Required Smarty library files (all in lib folder)

libs/
Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
internals/*.php (all of them)
plugins/*.php (all of them)

Smarty Website Structure

Following folders are essential in a smarty based website. templates has tpl files and templates_c stores cached pages (It is website cache). Smarty contains lib folder containing smarty software. Name of these default folders can be changed and it is recommended to change these and tell script accordingly templates directory and cache directory and location of Smarty.class.php.

templates_c
templates
Smarty

Images and css folders you will have to create, as every website has these. With these you can create a configs folder to store configuration files.

images
css
configs

Here’s how you create an instance of Smarty in your PHP scripts:

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
?>

How Smarty Works

After you have created above mentioned structure, the concept is to create PHP pages and place those in root and in templates folder create tpl files for these pages. There will be index.php file containing PHP code and variables at root directory of website and there will be index.tpl file in templates folder containing html code, design part of website and smarty code. We send PHP variables from index.php to index.tpl smarty code and then it mix things together in simple words and compile the page for you and build its cache too.

Sample index.php Code

<?php
require 'Smarty/libs/Smarty.class.php';
// $smarty->caching = false;
$smarty = new Smarty();
$smarty->display('index.tpl');
?>

OR

<?php
// error_reporting(E_ALL ^ E_NOTICE);
// display errors
// @ini_set("display_errors", "1");
require 'Smarty/libs/Smarty.class.php';
// $smarty->caching = false;
$smarty = new Smarty();
$smarty->assign("HOME", "class='selected'");
$smarty->display('index.tpl');
?>

Above we are sending variable from PHP to smarty for navigational element HOME. In tpl file we will write some smarty code which will apply class selected to that element.

Sample index.tpl Code

{include file="header.tpl" title="header"}
{include file="body.tpl" title="body"}
{include file="footer.tpl" title="footer"}

Above we included header, body and footer code in single index page.

Download smarty based website. Just upload it at your PHP enabled server or copy files to WAMP local server and browse index.php to see smarty in action.

Download Smarty Example

Related Posts

Previous post:

Next post: