A web engineer’s blog
10 Jul
Note: this is article is cut in parts, this is the the second one. Visit the first one if you don’t know what Smarty is and why to use it. The post supposes basic PHP programming skills.
Ok, last time we examined the goods and bads of using Smarty, this time we’ll install it.
First, make sure you got the right PHP version, it needs at least the 4.0.6. This can be seen by writing an empy page and writing in it, then calling that page.
Next, download the latest stable release from the smarty web site.
I prefer to have all my external libraries and programs in the /lib folder, I strongly racommend it, so decompress the source in the /lib/smarty folder.
I group all my site files in the /site folder, that’s very useful too, so create a site folder and put a php file in it, let’s call it page.php.
First line in you should configure and initialize smarty, then put your commands to produce some data, and at the end call smarty to output the data in the templates.
The reduced minimal sample is
<?
require (’/absolute/path/to/your/website/root/lib/smarty/Smarty.class.php’);
$smarty = new Smarty;
?>
Keep an eye on capitals, as the code is case sensitive! This sample initializes adds the smarty code on top of your php file by inclusion. You’ll need the absolute path to your smarty class, substitute as needed.
The smarty code defines a class, it explains to php what a "Smarty" is.
Second line actually defines a variable, called $smarty. This variable points to a new object called Smarty. An object is an "istantiation" of a class, speaking simple an "item" made by following the "instructions". You can have more than a Smarty object in a php file, but rarely you need to.
This Smarty item (let’s call it object again and from now on) has properties (attributes) and can execute commands.
Now let’s configure a thing or two. As I find it boring to always go checking out the document and site root and substituting, before going on let’s write some code to autodetect this.
add this code on top
define(’DR’, $_SERVER['DOCUMENT_ROOT']);
Now the constant DR contains our document root, we can then modify the subsequent line this way:
require (DR . ‘/lib/smarty/Smarty.class.php’);
each time you need the absolute path to your document root, you can refer to it by using DR.
Ok, this is all good, but if you load the script in the browser, it spits out a blank page.
What remains to do? Configure and execute some commands with the smarty object, of course! But before going on, I need to make a break for a security advice: templates doesn’t "need" to be in your document root, they can stay everywhere, and it’s highly suggested moving them outside this folder because they could be accessible from the web directly otherwise! Also, you’ll need to set some permissions, if you’re too loose it’s possible you enable complete strangers to write their files everywhere, compromising your hosting server.
Now that I did some information technology terrorism, we can go on!
To give Smarty a command, you use the PHP syntax
$smarty -> command(argument [...]);
while to set a property we use
$smarty -> property = value;
We’ll need to set up at least two directories, better three, and two of them should be writable by the web server (not the web users!).
So we create the /templates, /templates_compiled and /templates_cache folders, have templates_compiled and templates_cache writable (you can chmod them 770 from shell or even from the majority of the ftp clients, including the one in Dreamweaver!). Then we make smarty aware of the presence of these directories by adding these lines on bottom of the php file:
$smarty->template_dir = DR . ‘/templates’;
$smarty->compile_dir = DR . ‘/templates_compiled’;
$smarty->config_dir = DR . ‘/site’;
$smarty->cache_dir = DR . ‘/templates_cache’;
Perfect, and we added en passant the config_dir, that tells the Smarty object where to find auxiliary configuration files. We set it to the /site folder itself.
Now, to assign some templates variables, use this
$smarty->assign(’firstname’,'Giancarlo’);
$smarty->assign(’lastname’,'Di Massa’);
to load, parse and output a template, use
$smarty->display(’page.tpl’);
This will make the Smarty object go fetch the page.tpl file in the templates directory, so better have something here. Create a page.tpl file and put in it
Hi {$firstname} {$lastname}, how are you?
Save and reload the page.php file. It should output
Hi Giancarlo Di Massa, how are you?
Smarty substituted the two tags with the assigned variables in the page.php file!
If you are a programmer, go study the smarty syntax for programmers, it tells all the commands you can issue on the smarty object. If you’re a designer instead, there is a list of all the tags you can use in the templates, you can edit them as if they were static HTML files sprinkled with "programmer’s magic".
Most of the other template engines work the same way, for example the VlibTemplate or the RainTPL libraries.
One last word is about the MVC architectures. MVC stays for Model-View-Controller, is a way of separating content (the model) from the code (the controller) and the html (the view). A complete MVC system that takes care of all the aspects of data fetching and manipulation, from database queries to the output, is often called a framework. Smarty can be part of a MVC framework but it’s not itself one. Some good PHP frameworks are CakePHP and Symfony.
I use a MVC framework I did myself called Presenza.
Next time we’ll better organize our php code and start having something more complex of an Hello World template, so stay tuned.
No Response for "How to separe PHP code and HTML presentation with Smarty, part 2: installation"
My question is that can we do any mathmetical manipulation in smarty page.
Leave a reply