Smarty Template Engine using PHP

Standard

Smarty is a PHP template engine written by Monte Ohrt and Andrei Zmievski. Yet another template engine you say? The authors (and I too) would disagree. Smarty is different from the rest of the pack.

What differentiates Smarty from other template engines like FastTemplate and patTemplate is that Smarty compiles your templates into PHP scripts, eliminating the overhead incurred in parsing the templates every time they’re accessed.

This makes Smarty very scalable for large applications and high-traffic Websites …and if that didn’t make any sense to you, just take it that Smarty is very fast and would work well in stressful and high-traffic conditions!

smarty-engine

This post is about basic template engine management using Smarty with PHP. Smarty engine is an awesome tool, it saves your design development time. 9lession made a simple tutorial that how to implement Smarty for you PHP applications to follow basic standards and steps.

Smarty is a template engine for the PHP development language.
Why use Smarty?

-Separates the presentation logic from business logic ( Code and design are seperated)
-If we use core PHP with mixed with HTML then mess to manage.
-No PHP knowledge is required to manage smarty templates.
-Web designer and PHP developers can easily work and don’t blame each other. ( When they develop a big websites )

Smarty offers tools
– granular data caching
– template inheritance
– functional sandboxing to name a few
Where to find?

Download a package from smarty.net/download with your compatible PHP version.

How to install?
Unzip the downloaded smarty file into your appserv/www folder and run the application.

Basic syntax in the smarty
In the index.php file (In the root folder of your application )

a) Include the smarty class ( Which is libs folder).
require(‘libs/Smarty.class.php’);
b) Create object to the that smarty class
$smarty = new Smarty;
c) Assign variables
$smarty->assign(“var_name”, “Smarty”);

Here:
“var_name” is to use in the smarty template ( .tpl file)
“Smarty” is the value to that

Add Styles and Javascript files in templates files ( .tpl files)

Conditions

{literal}

function display(){
document.write(“Welcome to smarty”);
}

{ /literal}

Control Structure in smarty

{if (condition)}
—– statements —-
{/if}

{if (condition)}
—– statements —-
{else}
—– statements —-
{/if}

{if (condition)}
—– statements —-
{elseif (condition)}
—– statements —-
{/if}
{/if}
in the conditions: “eq” is for “=”, “neq” is for “!=”

Loops

{section name=i loop=$ptquestionary}
{$ptquestionary[i]}
{/section}

Develop a simple application using smarty : User registration process.

Database

CREATE TABLE USERS (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
fullname VARCHAR( 255 ) NOT NULL ,
user_name VARCHAR( 255 ) NOT NULL ,
password VARCHAR( 255 ) NOT NULL ,
created_on TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Config.php

Index.php

debugging = true;
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->assign(“title”, “User Registration using Smarty application”);
$smarty->display(‘index.tpl’);
?>

register.php

<?php
include("config.php");
if(isset($_POST))
{
$query = "INSERT INTO USERS(fullname,user_name,password) VALUES (' ".mysql_escape_string($_POST['fullname'])."', '".mysql_escape_string($_POST['user_name'])."','".md5($_POST['password'])."')";
$result = mysql_query($query);
if($result)
{
echo "window.location=’index.php?msg=successfully inserted ‘;”;
}
}

Templates files

header.tpl

{$title}
{literal}

body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#333333;
}

{/literal}

index.tpl

{include file=”header.tpl” title={$title}}

Name :
User Name :
Password :

{include file=”footer.tpl”}

footer.tpl

Few links developed in smarty

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s