SMARTY BASIC CONCEPT
This tutorial is moderated from http://www.9lessons.info/2011/09/smarty-template-engine-using-php.html
2.1) index.php
<HTML> <HEAD> <TITLE>User Registration using Smarty application</TITLE> <style type="text/css"> body{ font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333; } </style> </HEAD> <BODY > <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> </BODY> </HTML> |
2.2) 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>"; } } ?> |
2.3) config.php
<?php $dbHost = "localhost"; $dbUser = "root"; $dbPassword="root"; $dbName="smarter1"; $con = mysql_connect($dbHost,$dbUser,$dbPassword); $sel = mysql_select_db($dbName,$con) or mysql_error(); ?> |
3.1) Create a new database “smarter1”.
CREATE DATABASE `smarter1`; |
3.2) Create table “users”.
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 ); |
Download Smart Package from http://www.smarty.net/download and extract into webroot as smarter2.
Add the following files:
But the content will slightly differ from the previous steps.
2.1) index.php (different from previous codes)
<?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'); ?> |
2.2) register.php (same as previous codes)
<?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>"; } } ?> |
2.3) config.php (same as previous codes)
<?php $dbHost = "localhost"; $dbUser = "root"; $dbPassword="root"; $dbName="smarter1"; $con = mysql_connect($dbHost,$dbUser,$dbPassword); $sel = mysql_select_db($dbName,$con) or mysql_error(); ?> |
Add new template files in smarter2/templates/…
2.4) 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 > |
2.5) 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"} |
2.6) footer.tpl
</BODY> </HTML> |
3.1) Create a new database “smarter2”.
CREATE DATABASE `smarter2`; |
3.2) Create table “users”.
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 ); |