Smarty is a PHP Template engine that will allow you to create a PHP Backend without using full HTML within your PHP Files. Smarty will use a cache directory to store the template cache for each session and clear it once it reaches a certain age.
Smarty has build in loop functions to minimise the use of PHP Code from within your template documents. In this tutorial I am going to show you how to create a simple page To output an array using a loop in Smarty. The array will be outputted in a simple HTML Table to show how smarty works.
Ok So we need the smarty Libs to do this you can retreive the latest smarty distribution from Smarty : Template Engine
Once you have downloaded it Upload it onto your web server or put it in your WAMPP/LAMPP htdocs directory.
On your server create 2 new folders with the following configuration.
Ok now create a new file and call it config.php and place the follwoing code into the fileCode:Dir Name CHMOD ----------------------------------- html 665 cache 777
Now we have our basic smarty config setup we are ready to use smarty to output some data.Code:<?php
$dir = dirname(__FILE__); //Just to get the directory we are in
define ("SMARTY_DIR", "<WHERE YOU STORE SMARTY>"); //Use an Absolute path (/home/<user>/htdocs/smarty on linux)
require_once (SMARTY_DIR . "Smarty.class.php");
$smarty = new Smarty;
$smarty->compile_dir = "$dir/cache";
$smarty->template_dir = "$dir/html";
?>
Create a new file call it index.php this is where our aarray is gonna be and It will call out template file.
Now in the HTML Direcotry we need to create our array.tpl file. To do this make a new HTML file and save it as array.tpl in the html directory.Code:<?php
require_once ("./config.php");
$phpGods = array("Affix","Jordan","CodeCall");
$smarty->assign("gods", $phpGods); //Here we assign the Array to a smarty var
$smarty->display ("array.tpl"); //This will display the file array.tpl from our html directory
?>
In this file use the following code.
Now we have this done Upload your script and test it.HTML Code:<h1>PHP Gurus</h1> <div align="center"><table width="50%" height="50%> {section name=i loop=$gods} <tr> <td>{$gods[i]}</td> <tr> {/section} </table></div>
You will notice the loop I created using smarty. This is basically a for each loop from PHP.
Now you know the basics of smartyPHP hope you enjoyed my tutorial and will stop including HTML in your PHP Scripts
any questions email me : affix@fedoraproject.org
Excellent tutorial, +rep! I've been meaning to give Smarty a try.
Glad you like![]()
Good work mate+rep
What PHP framework do you use most commonly?
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
I write my own frameworks usually
Then is it really a framework?
Ever hang around @ #osdev on freenode? I'm in there 24/7 if you stop by.I do application and kernel development. Im working on my Own Distro atm
Jordan, I'll make a nice long post for you when I get home.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks