This post is about basic template engine management using Smarty with PHP. Smarty engine is an awesome tool, it saves your design development time. My friend Anil Panigrahi made a simple tutorial that how to implement Smarty for you PHP applications to follow basic standards and steps.
Download Script
Author
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)
{literal}
<link rel="stylesheet" href="css/style.css" type="text/css">
<script>
function display(){
document.write(“Welcome to smarty”);
}
</script>
{ /literal}
<link rel="stylesheet" href="css/style.css" type="text/css">
<script>
function display(){
document.write(“Welcome to smarty”);
}
</script>
{ /literal}
Control Structure in smarty
Conditions
{if (condition)}
----- statements ----
{/if}
{if (condition)}
----- statements ----
{else}
----- statements ----
{/if}
{if (condition)}
----- statements ----
{elseif (condition)}
----- statements ----
{/if}
{/if}
----- 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}
{$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
);
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
<?php
$dbHost = “localhost”;
$dbUser = ”username”;
$dbPassword=”password”;
$dbName=”database”;
$con = mysql_connect($dbHost,$dbUser,$dbPassword);
$sel = mysql_select_db($dbName,$con) or mysql_error();
?>
$dbHost = “localhost”;
$dbUser = ”username”;
$dbPassword=”password”;
$dbName=”database”;
$con = mysql_connect($dbHost,$dbUser,$dbPassword);
$sel = mysql_select_db($dbName,$con) or mysql_error();
?>
Index.php
<?php
include(“libs/Smarty.class.php”);
include(“config.php”);
$smarty = new Smarty;
$smarty->debugging = true;
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->assign("title", "User Registration using Smarty application");
$smarty->display('index.tpl');
?>
include(“libs/Smarty.class.php”);
include(“config.php”);
$smarty = new Smarty;
$smarty->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 "<script>window.location='index.php?msg=successfully inserted ';</script>";
}
}
?>
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 "<script>window.location='index.php?msg=successfully inserted ';</script>";
}
}
?>
Templates files
header.tpl
<HTML>
<HEAD>
<TITLE>{$title}</TITLE>
{literal}
<style type="text/css">
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#333333;
}
</style>
{/literal}
</HEAD>
<BODY >
<HEAD>
<TITLE>{$title}</TITLE>
{literal}
<style type="text/css">
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#333333;
}
</style>
{/literal}
</HEAD>
<BODY >
index.tpl
{include file="header.tpl" title={$title}}
<form method=”post” action=”register.php”>
<div>
<div>Name : <input type=”text” name=”fullname” id=”fullname”></div>
<div>User Name : <input type=”text” name=”user_name” id=”user_name”></div>
<div>Password : <input type=”text” name=”password” id=”password”></div>
<div><input type=”submit” name=”submit” value=”submit” ></div>
</div>
</form>
{include file="footer.tpl"}
<form method=”post” action=”register.php”>
<div>
<div>Name : <input type=”text” name=”fullname” id=”fullname”></div>
<div>User Name : <input type=”text” name=”user_name” id=”user_name”></div>
<div>Password : <input type=”text” name=”password” id=”password”></div>
<div><input type=”submit” name=”submit” value=”submit” ></div>
</div>
</form>
{include file="footer.tpl"}
footer.tpl
</BODY>
</HTML>
</HTML>
Few links developed in smarty
How to implement google map in smarty?
How to integrate fckeditor in smarty application?
Hi Anil, Very helpful post
ReplyDeletethanks for nice tutorial :)
ReplyDeleteHi Anil This is very nice introduction of smarty,
ReplyDeleteI am a new in this PHP development field , I have one question , can we use existing website as template to create another many such website, for eg if I created a website for a school , and it contain all functionality that a school can think of ........ So now I want to just copy this site somehow and give this site to another school that have same requirements but database different ......... hope I am able to explain my question
Thanks a Lot
Gunjan
Good and it is easy to follow
ReplyDeleteThank you for this beautiful tutorial. :-)
ReplyDeleteI find foreach loop much more convenient then section.
ReplyDeleteGreat post!
ReplyDeleteReally love the simplicity of Smarty; things get simplified -- very easy to learn!
{foreach}{/foreach} is better and convenient over {section}{/section}. I very good introduction to smarty. Keep it up.
ReplyDeletecan you post a tutorial for pagination using smarty?
ReplyDeletesuper article
ReplyDeletethanks
ReplyDeleteVery nice post!
ReplyDeleteReally more beneficial thing shared here by you.
ReplyDeleteReally more beneficial thing shared here by you.
ReplyDeleteNice post for beginners
ReplyDeleteWhat do you think about Dwoo and twig ?
ReplyDeleteThank you all...
ReplyDeleteThanks a lot for the tutorial, it gave me a really good understanding about the smarty even though I have worked with smarty before once
ReplyDeleteDoubt: how to set value attribute of input tag to the database value in index.tpl? input type=”text” name=”fullname” id=”fullname” value="____" when i am editing a value
ReplyDeleteplease refer
ReplyDeleteinput name="first_name" id="first_name" value="{$udata.first_name}" type="text"
udata is asign in php file and first_name is table field name
dont have demo? plsse... i want to learn this smarty more n more
ReplyDeleteadd_edit.php file
ReplyDeleteinclude(“libs/Smarty.class.php”);
include(“config.php”);
..
..
$data=mysql_fetch_assoc($userdata);
$smarty-> assign("udata",$data);
$smarty-> display('add_edit.tpl');
add_edit.tpl
..
..
input name="first_name" id="first_name" value="{$udata.first_name}" type="text"
..
or
input name="first_name" id="first_name" value="{if $udata.first_name}{$udata.first_name}{/if}" type="text"
input type="submit" name="Submit" value="Save"
very nice me also a beginner got a lot from this thanks
ReplyDeleteHello All,
ReplyDeleteHave any plugin for smarty?
Can we implement or install plugin for smarty such as word press
hi,
ReplyDeletehow can i disable smarty debug console pop up window. and please creata a admin panel script for editing I hope you getting my point please help me.
Thanks
Nice tutorial Anil...
ReplyDeletewonderful tutorial.. really helpful.
ReplyDeleteSmarty is not so good... it's very "heavy" better use RainTPL is much faster than Smarty
ReplyDeleteVery nice, yeah Smarty is the best for the PHP template engine
ReplyDeletevery easy tutorial thank u mahesh
ReplyDeleteNice Smarty tutorial Anil...
ReplyDeletevery good tutorial for beginers... Ravi
ReplyDeletenice
ReplyDeletevery helpful and simple understanding is done..
ReplyDeleteVery Nice
ReplyDeleteThank you,it ll use very much
ReplyDeletehello....sir
ReplyDeleteplease creating demo for login system in smarty mvc framework with validation