WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - השוואת מחירים בסופר
ZeroLag.com
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Smarty Introduction
Categories : PHP, Smarty, Beginner Guides
Joe Crawford Jr.
Joe Crawford Jr.
Date : 2004-06-05
Grade : 4 of 5 (graded 3 times)
Viewed : 30191
Search : More Articles by Joe Crawford Jr.
Action : Grade This Article
Tools : My Favotite Articles


Submit your own code examples 
 


What is smarty?

Smarty is a template engine that you can use for your websites. If used properly it is used to separate your PHP code from your HTML code. When a file is called from the web, the php file is parsed and smarty compiles the results from the template files used. When that same page is called and the template files have not been altered smarty will use the compiled version which causes the server to not need to recompile the pages. Smarty also has the ability to use cache.


Why is it important to separate design from application code?

When you have several people working on a project designers and programmers can work together. When a designer changes a template file they are not touching the programming, hence they cannot break the functionality. This will ensure that the code will run smooth and without errors. Designers can change the template files all they want. If a site needs a redesign the designer can do this without calling upon the programmer to help. Although smarty separates application code from the templates it does
not necessarily separate logic, templates can some logic such as if statements. Smarty includes several more logic functions that will be explained at a later time.

You can get smarty from http://smarty.php.net/


Installing

When you are installing smarty you should only install one copy per website, that is all you will need. When I setup smarty I usually create directories like this :


/* The main smarty directory */
/home/user/smarty/

/* The directory for your template files */
/home/user/smarty/templates/

/* The directory smarty places the compiled templates in CHMOD this to 777 */
/home/user/smarty/templates_c/

/* The configuration directory, for more advanced users */
/home/user/smarty/configs/

/* The cache directory where cached files are stored by smarty */
/home/user/smarty/cache/

next I upload the smarty files to a directory like

/home/user/public_html/include/class/smarty/


now onto how to use smarty.

Smarty has several functions at your disposal however the main ones you will use are assign() and display()
After uploading smarty create a new file called index.php in that file add the following code


<?
/* Make sure the case is exact, smarty has a strange naming convention, I usually use all lowercase for my filenames */
include('include/class/smarty/Smarty.class.php');

$smarty = new smarty();

/* time for some setup */
$smarty->template_dir = '/home/user/smarty/templates';
$smarty->compile_dir = '/home/user/smarty/templates_c';
$smarty->config_dir = '/home/user/smarty/configs';
$smarty->cache_dir = '/home/user/smarty/cache';

/* turn debugging off, this should be on while developing */
$smarty->debugging = false;

/* let's force smarty to compile each file, this should be FALSE in a production environment
I have set it to true since you will be in development mode.
*/
$smarty->force_compile = true;

$smarty->assign('name', 'Joe');
$smarty->display('index.tpl');

?>




Now that you have your index file you need to create the index.tpl file that we called in the display() function.
add this file to /home/user/smarty/templates.

DO NOT USE PHP <? OR ?> tags in TPL files Add the following code :


<html>
<head>
<title>Smarty :: Test File</title>
</head>
<body>
My name is {$name}.
</body>
</html>


let's go over the tpl file for a moment, there is an odd tag in there {$name}. When creating smarty template files you need to use the curly braces when accessing variables that you set using the assign() function. using {$name} in the tpl file tells smarty to replace {$name} with the actual value of $name in the php file.

When you run this script you should see output like


My name is Joe.


unless you changed anything before running the code.

Well that was easy enough, now onto some more complex stuff. Do you remember be stating that smarty can have logic within the template files? We are now going to discus how to use control structures.


Using The If Statement.

When using an if you need to have a variable to compare and a closing if tag. An if statement looks like the following :


{if $name}
My name is {$name}.
{/if}


This works exactly like the if statement in PHP. Notice that in the {if $name} I don't have curly braces around $name as well. {if {$name}} will cause an error. You CANNOT create an if statement like that. Notice the / in {/if} this is needed to tell smarty that it is the end of the if statement. Don't forget that as it will cause an error ;)


{if $name != ''}
My name is {$name}.
{/if}


The code above would work just the same as the previous code however this shows how you can take advantage of comparison operators in smarty.

Along with the if statement comes the elseif and else statements, you could do something like this :


{if $name == 'Ted'}
My name is Ted.
{elseif $name == 'John'}
My name is John.
{else}
My name is {$name}.
{/if}


The above code is just a small example and not very practical however it get's the idea across of how you can fully use an if statement in smarty.

This is just a starter on using Smarty, I will write more articles of more complexity down the road however I urge you to read Smarty's documentation if you want to fully take advantage of it's capabilities. You can get the documentation from :
http://www.weberdev.com/Manuals/Index.html#Smarty









Beginners guide to PHP and MySQL - Creating a simple guest book
Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
PHP 101 Part 7 of 15 : The Bear Necessities
Categories : PHP, Beginner Guides, Object Oriented, PHP Classes
Counting - Creating a simple counter
Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
PHP Classes And Objects: A Guide To Development
Categories : PHP, PHP Classes, Object Oriented, Beginner Guides
Beginners Guide to PHP - Introduction to cookies
Categories : Beginner Guides, Cookies, To PHP, PHP
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Counting - Creating a GIF based counter using PHP and MySQL
Categories : Beginner Guides, PHP, To PHP, To MySQL, MySQL
How TO Install PHP, Apache and MySQL on Linux or Unix
Categories : PHP, MySQL, Apache, Installation, Beginner Guides
PHP 101 Part 10 of 15 : A Session In The Cookie Jar
Categories : PHP, Beginner Guides, Cookies, Sessions
Counting - Creating a more sophisticated GIF based counter using PHP and MySQL
Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL
PHP 101 Part 15 of 15 : No News Is Good News
Categories : PHP, Beginner Guides, Content Management
Who's Linking?
Categories : PHP, Beginner Guides, To PHP