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
-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
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)
Control Structure in smarty
Conditions
in the conditions: “eq” is for “=”, “neq” is for “!=”
Loops
Database
Config.php
Index.php
register.php
header.tpl
index.tpl
footer.tpl
Few links developed in smarty
How to implement google map in smarty?
How to integrate fckeditor in smarty application?
Download Script
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?
No comments:
Post a Comment