PHP Website Development
Home | Contact Us | Sitemap

Custom Search

What is SMARTY?


Smarty is a PHP template engine. It is architecture to build PHP web application with built-in template compiling, and optional output caching features. It facilitates a manageable way to separate application logic and content from its presentation. Smarty parses your templates and creates PHP scripts from them (instead of binaries, as in general programming). Then, when your Web page is viewed, Smarty reads from these PHP scripts instead of pulling the templates themselves, which saves the work of having to parse your templates again. The good thing about this is that you don't even have to know the PHP scripts are there, nor how compiling works. It's all hidden from view. Smarty also features built-in caching of your template outputs. Smarty caches the output of the template contents, saving the overhead expense involved in retrieving your data from a data source. You can control SMARTY to allow or not to cache the page.

Historically the project was started in late 2000 by Monte Ohrt and Andrei Zmievski, and was the result of a failed attempt at writing an all-purpose template extension for PHP in C. They decided to start from scratch and rewrite it as a PHP class. Recent development has been done by Monte and Messju Mohr, and involved adding object support, optimizing the compiler, and enhancing custom function support.

Some of the features of smarty include,
  • It 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 make custom functions and custom variable modifiers, so the template language is extremely extensible.
  • Configurable template delimiter tag syntax, so you can use {}, {{}}, , etc.
  • The if/elseif/else/endif constructs are passed to the PHP parser, so the {if ...} expression syntax can be as simple or as complex as you like



SMARTY Basic Syntax
 
Smarty requires a web server running PHP 4.0.6 or greater. We assume that you have successfully installed SMARTY to your application. To install, copy all the files under the libs/ directory (From the pack you downloaded) to a directory that is in your PHP include_path, or set the SMARTY_DIR constant and put them in this directory. For each application using Smarty, create a "templates", "configs", and a "templates_c" directory, be sure to set the appropriate directory settings in Smarty for them. Be sure the "templates_c"directory is writable by your web server user (usually nobody). If you are using Smarty's built-in caching, create a "cache" directory and also make it writable by your web server.

Example #1 Setting The Envirenment And Printing "Hello World!"

   first.php
<?php
require_once('Smarty/libs/Smarty.class.php');
$smarty = new Smarty();

$smarty->compile_check = true;
$smarty->debugging = true // Making debug on
$smarty->template_dir = $template_dir;//'/web/www.domain.com/Smarty/templates';
$smarty->compile_dir = $compile_dir;//'/web/www.domain.com/smarty/templates_c';
$smarty->cache_dir = $cache_dir; //'/web/www.domain.com/smarty/cache';
$smarty->config_dir = $config_dir; //'/web/www.domain.com/smarty/configs';

$smarty->assign('hello',"Hello World!");
$smarty->display('first.tpl');
?>
first.tpl
<!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=iso-8859-1" />
<title>
First Smarty Example</title>
{literal}
<script type="text/javascript">
alert('Hello');
</script>
{/literal}
</head>

<body>

{$hello}
</body>
</html>


The basic foreach loop syntax is:

Example #2 Basic loop structure

   loop.php
<?php
require_once('Smarty/libs/Smarty.class.php');
$smarty = new Smarty();

$smarty->compile_check = true;
$smarty->debugging = true // Making debug on
$smarty->template_dir = $template_dir;//'/web/www.domain.com/Smarty/templates';
$smarty->compile_dir = $compile_dir;//'/web/www.domain.com/smarty/templates_c';
$smarty->cache_dir = $cache_dir; //'/web/www.domain.com/smarty/cache';
$smarty->config_dir = $config_dir; //'/web/www.domain.com/smarty/configs';

$arr = array(1000, 1001, 1002);
$smarty->assign('myArray',$arr);
$smarty->display('loop.tpl');
?>
loop.tpl
<!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=iso-8859-1" />
<title>
Smarty Basic Loop Structure Example</title>
</head>

<body>


<ul>
{foreach from=$myArray item=foo}
   <li>{$foo}</li>
{/foreach}
</ul>

</body>
</html>
his code would generate the output,

  • 1000
  • 1001
  • 1002
Our other websites
Free PHP Source Code Download    Free Bollywood News & Downloads   Free Funny SMS Text Messages
 
Home | About Us | Privacy Policy | Site Map | Contact Us

Copyright © 2009-2010 PHPWEBS