Smarty Template Engine using PHP
Wall Script
Wall Script
Monday, September 05, 2011

Smarty Template Engine using PHP

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.

Smarty Template Engine using PHP


Download Script

Author
Anil Kumar Panigrahi
Anil Kumar Panigrahi
Software Engineer & Freelance
Visakhapatnam, INDIA
anil2u.info

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}

Control Structure in smarty

Conditions
{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
<?php
$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');
?>

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>";
}
}
?>

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 > 

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"}

footer.tpl
</BODY>
</HTML>

Few links developed in smarty
How to implement google map in smarty?
How to integrate fckeditor in smarty application?
web notification

37 comments:

  1. Hi Anil This is very nice introduction of smarty,

    I 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

    ReplyDelete
  2. Thank you for this beautiful tutorial. :-)

    ReplyDelete
  3. I find foreach loop much more convenient then section.

    ReplyDelete
  4. Great post!

    Really love the simplicity of Smarty; things get simplified -- very easy to learn!

    ReplyDelete
  5. {foreach}{/foreach} is better and convenient over {section}{/section}. I very good introduction to smarty. Keep it up.

    ReplyDelete
  6. can you post a tutorial for pagination using smarty?

    ReplyDelete
  7. Really more beneficial thing shared here by you.

    ReplyDelete
  8. Really more beneficial thing shared here by you.

    ReplyDelete
  9. What do you think about Dwoo and twig ?

    ReplyDelete
  10. Thanks a lot for the tutorial, it gave me a really good understanding about the smarty even though I have worked with smarty before once

    ReplyDelete
  11. Doubt: 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

    ReplyDelete
  12. please refer

    input 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

    ReplyDelete
  13. dont have demo? plsse... i want to learn this smarty more n more

    ReplyDelete
  14. add_edit.php file

    include(“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"

    ReplyDelete
  15. very nice me also a beginner got a lot from this thanks

    ReplyDelete
  16. Hello All,

    Have any plugin for smarty?
    Can we implement or install plugin for smarty such as word press

    ReplyDelete
  17. hi,
    how 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

    ReplyDelete
  18. Nice tutorial Anil...

    ReplyDelete
  19. wonderful tutorial.. really helpful.

    ReplyDelete
  20. Smarty is not so good... it's very "heavy" better use RainTPL is much faster than Smarty

    ReplyDelete
  21. Very nice, yeah Smarty is the best for the PHP template engine

    ReplyDelete
  22. very easy tutorial thank u mahesh

    ReplyDelete
  23. very good tutorial for beginers... Ravi

    ReplyDelete
  24. very helpful and simple understanding is done..



    ReplyDelete
  25. hello....sir

    please creating demo for login system in smarty mvc framework with validation

    ReplyDelete

mailxengine Youtueb channel
Make in India
X