by Hiroshi on October 27th, 2008

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

  • Share/Bookmark

,

Leave a Review

Incoming Searches

smarty ebook, smarty blog script, where add header and footer and folder action in smarty, smarty cache tutorial, smarty cache not working on live server, smarty books free, smarty assign page variable tpls, smarty and php examples, simple php login script with smarty, script counter smarty, phpmagic templates, php tpl books, php smarty and cms how it works, Smarty class php photo gallery, Smarty Class Variables example, upload file php smarty, TPL smarty variable in PHP code, smarty web site structure, Smarty Templates books free online, smarty selected with single index, smarty scripts, smarty images slideshow, smarty gallery template, smarty free functions, smarty ebook download, smarty codeigniter header footer, php secure smarty cms, php require_once smarty_compiler class php to use two instances of smarty, php cms smarty book, free smarty CMS, free php cms tutorial with caching, free php cms tpl, free download smarty tutorials, free download smarty tutorial, free cms php smarty, ebooks free download of smarty, default tpl code smarty, cms in smarty, build php cms ebook, books Script PHP contains the evaluation and recommendation, free smarty ebook, free tpl code, function slideshow smarty, online ebooks for smarty free, login script smarty, is it possible to work in smarty without templates_c folder, index php tpl smarty, include tpl files from theme wordpress, include php in tpl, htaccess in smarty, how to make website with smarty, how to include tpl file from different folder in wordpress, how to create web Smarty, how to build a footer tpl in cms, book on smarty