Simple Blog – The Index

Simple Blog is a multi-part series about developing data-driven application with PHP and MySql. Check out the intro for details about this unique approach.


Simple Blog – Example 7: Smarty

Simple Blog is a multi-part series. Check out The Index

In the previous example, we improved our application’s models by rewriting the Database and Post classes using PHP Data Objects. In this example, we’ll shift focus to our application’s views. Until now, we’ve been using PHP’s own engine to render our view pages. Our view pages were made up of HTML interspersed with small block of PHP to import header and footer markup, iterate through arrays, and display the contents of variables. In this example, will integrate the Smarty Template Engine into our application which promises to truly separate presentation logic (HTML/CSS) from application logic (PHP).

In the process of researching this post, I hit up to read up on the advantages of implementing their template engine. They claim that Smarty makes the development process easier, faster, and more maintainable. On the flip side, Smarty openly acknowledges that installing their template engine results in slower script execution as compared to native PHP. Obviously, introducing an additional layer of complexity to page rendering will result in slower execution; but how much slower? We’ll get to that …

So, let’s take a look at what Smarty has to offer. We’ll start by looking at our configuration file, where we’ve made a few administrative changes to get Smarty working. FYI, if you’re following along, take a look at Smarty’s quick install guide before proceeding.

Configuration File

Instead of listing the entire config file, I’d like to highlight just the lines relevant to installing Smarty. First, we define an absolute path to the Smarty directory:

define('SMARTY_PATH', INCLUDE_PATH.'smarty'.DS);

Next, we import the Smarty class:


Continue reading