![]() Join Up! 96812 members and counting! |
|
|||
Smarty Templating System
Ben Robinson
Introduction
The Smarty templating system is a fantastic framework for architecting php-based websites, especially in a collaborative development environment. This article will examine the benefits of Smarty, as well as delve into some basic examples of its implementation. We will also point the way to the available resources and community based around the system. First, let's make the case for Smarty.
Why smarty?
Setup
Download the code sample and extract it to your web directory. Before you get started, open up demo/index.php and look at line three:
require '../libs/Smarty.class.php' the default (from the regular smarty download) wants you to use your /var/lib/php/smarty directory, but if you have a virtual server you do not have access to this. The code example for this article has this relative path set for you. You also need to add the directory in the demo folder called templates_c and make sure it's writable, as the program will be expecting it. This directory is where smarty loads your template files and compiles them for quicker execution (refer to the the smarty manual for more info about caching).
You should be able to load the page from http://
The code example for this article has the guestbook demo integrated with it, also containing a tweak to use mysqli instead of PEAR DB, so if you download the original demo archive for the guestbook app, you'll see some subtle differences. Don't forget to run the guestbook sql script so you have the database there! (demo/guestbook/sql/)
Let's take a look at the template file for the guestbook page (demo/guestbook/templates/guestbook.tpl):
The dynamic tags look a little strange at first, but the more you get accustomed to them, the more you will appreciate the simplicity of the smarty tagging system (which, as mentioned, is highly customizable as well).
Take note of the $data variable; this is passed from the index page in the command:
which does this (in libs/guestbook_lib.php):
The assign command is using a smarty assignment method to allow your template file to access the $data array obtained from the getEntries function, which just does a select * on your guestbook table and then prepares the array for smarty to loop through:
the next line:
tells the system to display the guestbook template.
Taking a close look at how this code is organized will really get you started in how sophisticated you can get with this system. Take special note again of how simple the index page and template files are in this application! As a coder, you would have full control over logical execution of code on the index page and all background class files. The only unknowns would be if your designer mucks with your smarty tags in the template files, but that won't be happening, now, will it?
Conclusions and Suggestions
I would highly recommend perusing the Smarty documentation (chm file included in sample code download). It is very well written and goes into great detail about what you can do with this lightweight but extremely elegant framework. Also, while your visiting smarty.php.net, take a look in the forums to see what kind of buzz is going around, there's quite a bit of activity and support for this system. The wiki is also great, and has a pretty impressive list of plugins for everything from dreamweaver usage to form validation. There are also job requisitions for people who need specialized work done in smarty on the bulletin boards!
All in all, I personally think smarty is one of the best ways available to begin your own homegrown cms. In many ways, you can save yourself the headache of using a pre-designed cms package and reverse engineering it down or sideways to what you need it to do. Not to balk at these packages, some of them are fantastic, and very easy to implement (one of my favorite ones out there is cmsmadesimple), or at least have some great designs that you can use bits and pieces of in your own framework. Good luck!
About Ben Robinson
Ben Robinson is an open source coder who enjoys working on dynamic web applications, especially in PHP and mysql, in the L.A.M.P. environment. His website is TierraLogic Systems (http://www.tierralogic.com/) if you’d like to pay him a visit.
|