This tutorial will talk the reader through the smarty, its ups and downs and how to use smarty within your own website. By the end of this tutorial the reader should have an unbiased view of how to use Smarty and the coding knowledge required to implement the Smarty framework.
What is Smarty?
Smarty is NOT a template engine, it states on the website [Smarty Website] that Smarty is a framework for creating templates. Yes, sure it comes compliant with a template engine but Smarty itself is not the template engine. Smarty is all the little function for making template building easier.
There are a lot of sites out there that hate Smarty [Here] but I think it’s a great little piece of programming. Everyone seems to think Smarty is:
- Bug ridden
- And just, well, shit
All of the above are valid and all can be supported. Some say that there are superior template frameworks out there but when I use them I find the framework makes my site load up to twice as long. Sure Smarty is not the prettiest girl in the group but you should never judge a book by its cover, she could be a really sexy girl but dead on the inside…
So now your probably asking “So how do I choose which is best?” Maybe you should go for no framework at all…
This tutorial will tell you how to decide whether you need to use Smarty or not.
Now the first thing to talk about is what to ask when building a site and deciding whether or not to use a template framework. There are a number of questions you can ask yourself:
- How big is my site?
- Will pages need to conform to a basic layout?
- Am I making a multilingual site?
- How many users would be visiting this site daily?
These are just some questions but if you can answer question 1 as big and question 2 as yes then your in with a good chance of needing Smarty. There is a word of caution though, if you are building a site which is viewed by millions a day you might not want to use a template framework at all…”Why?” Because it adds an extra layer and so extra weight and extra weight means extra load on the server.
How do I install Smarty into my Site?
Okay, so you have decided whether you need Smarty or not (in basic terms). Now you need to know how to use Smarty.
You first need to install Smarty, so depending on which distro you use you apt-get install smarty. Once that has been done smarty will be installed to /usr/share/php/. This directory will not function correctly at first which is why you need to add its location to the php.ini. Add the folder as a extension folder, if you search for “/usr/share/php” you will find a row that just needs “uncommenting”. If you uncomment this row the shared folder will function correctly. All template frameworks and other PHP related stuff will always be installed to this directory.
After you have installed Smarty you will need to bind it to your website.
//Include the smarty class from the shared folder. No need to state location of shared folder it will act as a folder of the website root. require('smarty/Smarty.class.php'); //Create the new smarty object to work with $smarty = new Smarty; //Assign all the working directories and such like. All of these are best to be specified. template_dir is the most important variable to assign since it is the location of your template files. config_dir contains any special Smarty and also non-Smarty configuration files required and compile_dir can not only be used for cache but the templates are also compiled in the directory for viewing. It is best to leave the compile directory outside of the immediate web root. $stage_root_path = getcwd() . "/"; $smarty->template_dir = $stage_root_path . 'styles/default/template'; $smarty->config_dir = $stage_root_path . 'styles/default/configs'; $smarty->compile_dir = '/usr/share/php/smarty/templates_c';
The script above will allow you create a new smarty object. Now you are almost ready to start Smarty-ing. You have the smarty object assigned now you need to know how to use it in order to build templates.
Examples of How to Use the Smarty Object
You will find this link particularly useful for getting Smarty examples. This will teach you the absolute basics of Smarty-ing. Enjoy :).