|
|
|
|
|
|
| |
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 |
| |
|
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
|
|
| |
| PHP 101 Part 8 of 15 : Databases and Other Animals Categories : PHP, Beginner Guides, Databases | | | Beginners guide to PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, Installation | | | Who's Linking? Categories : PHP, Beginner Guides, To PHP | | | PHP 101 Part 9 of 15 : SQLite My Fire! Categories : PHP, Beginner Guides, Databases, SQLite | | | Grabb'n and Pars'n Categories : Beginner Guides, PHP, To PHP | | | PHP 101 Part 15 of 15 : No News Is Good News Categories : PHP, Beginner Guides, Content Management | | | PHP 101 Part 10 of 15 : A Session In The Cookie Jar Categories : PHP, Beginner Guides, Cookies, Sessions | | | PHP 101 Part 12 of 15 : Bugging Out Categories : PHP, Beginner Guides, Errors and Logging | | | PHP 101 Part 2 of 15 : Calling All Operators Categories : PHP, Beginner Guides, Operators | | | PHP 101 Part 5 of 15 : Rank And File Categories : PHP, Beginner Guides, Filesystem | | | Beginners guide to PHP and MySQL - Creating a simple guest book Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL | | | Counting - Creating a simple counter Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL | | | PHP 101 Part 7 of 15 : The Bear Necessities Categories : PHP, Beginner Guides, Object Oriented, PHP Classes | | | PHP and OOP Categories : PHP, Object Oriented, Beginner Guides | | | PHP 101 Part 13 of 15 : The Trashman Cometh Categories : PHP, Beginner Guides, Data Validation | |
| |
|
|