Templating has been gaining in popularity for years among web developers, especially those working on portals or business sites that may have their web pages tied in with complex business rules. Now those same benefits from templating are coming to PHP. This article, originally appearing at DotGeek.org, looks at Smarty, one of the leading PHP templating engines available.
Benefits of PHP Templating
Web design and programming are closely related and yet are very different. Designers speak in such languages as HTML and CSS, and programmers are often heard speaking in the tongues of PHP and SQL. Design focuses primarily on presentation logic, and programming focuses primarily upon business logic. Separating these processes in web development cycles helps to achieve rapid application development goals while providing for website maintainability.
Changes to business logic require programmers to modify server-side code such as PHP. By using a templating system, the programmers can change the business logic implementation without having to make changes to the files that contain the presentation logic and designs in HTML and CSS. Changes to presentation logic and design require designers to change to these files, which, without a templating system, may also contain server-side code like PHP.
With a templating system, however, designers are not burdened with the business logic code that programmers have written. Designers make changes to the website templates, using a simple templating language, to access data from the business logic layer. In summary, a templating engine separates the complex business logic (PHP) from the presentation logic (HTML), allowing designers and programmers to interoperate more effectively.
Given the importance of website templating, there are a myriad of options from which to choose a template system implementation. Many individual members of the Open Source software development community have contributed simple and functional versions of PHP-based templating systems. Some are more suited for particular purposes than others. You may have some templating system in place, having an application programming interface (API), whether via function calls on included libraries or method invocation on various template-related objects.
Get Smarty: Getting Started
Whether or not you have experience with one or more PHP templating systems, if you are already using or are considering website templates, Smarty is definitely worth a closer look. It is perhaps the most functional templating engine available for PHP, and, since the Smarty engine uses PHP and the Zend Engine, your scripts run with minimal additional overhead than without a templating engine. Smarty runs well and efficiently. It even has partial page caching abilities for those who wish to fully optimize their templated websites.
This OET look at Smarty arises from an article originally published at Dotgeek.org, a PHP developers portal developed by gurugeek.
You can download Smarty from PHP.net’s website. Smarty is well documented and is part of the php.net family of sites. The author has experience with Smarty and recommends that both newbies and veterans alike refer to the installation instructions bundled with Smarty and the online documentation. Smarty runs on PHP versions 4.0.6 and later.
Smarty’s functionality is accessed from PHP via a simple object-oriented interface. programmers can easily build new websites that utilize the Smarty templating engine. Web developers can rapidly integrate Smarty into existing websites, as well.
Smarty can be implemented in several ways. To get started, the author suggests that each web application receive a separate set of Smarty folders (i.e., the cache, compile, config, and template hierarchies) as part of a well-planned implementation. So, to get started:
1. Create an instance of the Smarty object in the PHP this way:
require_once('Smarty.class.php'); // use absolute path here, if necessary $smarty = new Smarty;
2.Because Smarty uses several directories for its operation, it needs to be told where the directories are located.
Among the directories Smarty uses are: (a) the template directory, where it looks for Smarty templates (.tpl files); (b) the compile directory, where Smarty stores intermediary PHP code; (c) the config directory, where configuration directives may be located; and the (d) cache directory, where cached page contents are stored.
// using absolute paths is best here $smarty->template_dir = '/www/example.com/webapp/template/'; $smarty->compile_dir = '/www/example.com/webapp/compile/'; $smarty->config_dir = '/www/example.com/webapp/config/'; $smarty->cache_dir = '/www/example.com/webapp/cache/';
3. At this stage, it’s useful to exploit some of Smarty’s many options and behavioral bells and whistles.
A particularly useful option, especially in hosting environments or wherever PHP safe mode may be enabled, concerns Smarty’s creation of subdirectories on the web server. Setting the use_sub_dirs option (a property of the Smarty object) to false enables Smarty to operate without having to create subdirectories on the web server. The Smarty default is to use subdirectories. To change this:
$smarty->use_sub_dirs = false;
4. One way to handle this “options setting” is to extend the Smarty class and use either the constructor (or an initialization method) to make Smarty play nicely with your web application. As an example:
Smarty_WebApp.class.php: // require the Smarty class require_once('Smarty.class.php'); // extend the Smarty class class Smarty_WebApp extends Smarty {
// class constructor runs automatically for each newly created instance function Smarty_WebApp() { // create the Smarty object $this->Smarty(); // set Smarty folders $this->template_dir = '/www/example.com/webapp/template/'; $this->compile_dir = '/www/example.com/webapp/compile/'; $this->config_dir = '/www/example.com/webapp/config/'; $this->cache_dir = '/www/example.com/webapp/cache/'; // set other Smarty options $this->use_sub_dirs = false; } }
5. Now that set-up is complete, it is time to work directly with Smarty!
As an example, PHP assigns a couple of variables for access from a Smarty template, which is then displayed:
index.php: // require the Smarty_WebApp class require_once('Smarty_WebApp.class.php'); // create the Smarty_WebApp object $smarty = new Smarty_WebApp(); // assign values from PHP $smarty->assign('title', 'My Web Application'); $smarty->assign('place', 'world'); // display a template $smarty->display('index.tpl'); index.tpl:
<html>
<head><title>{ $title }</title></head>
<body>Hello, { $place }!</body>
The Smarty Results
When a web browser accesses index.php, the resulting page would have the title and the place dynamically assigned from PHP. You can see how the content is dynamically assigned from PHP and how the presentation logic is contained in the page template. The template accesses the dynamic data without regard to business logic implementation.
The idea can be extended to assign data fetched from a database, for example, and this hints at the real power of Smarty. Its simple templating language is fully capable of rendering data tables and populating HTML forms. The above example is a minimalistic and trivial use of Smarty. A fully developed web application, of course, would make much further use of the robust capabilities that Smarty offers. Through using it, website development can maintain higher levels of flexibility and separation of presentation logic from business logic.
Though Smarty’s rich feature set leaves little to be desired, Smarty is extensible, too. Smarty enables developers to write their own specific extensions to Smarty where special needs must be met.
Darby Felton is a PHP programmer, and a contributor to www.dotgeek.org.. Find other Smarty code samples and support here