« eAccelerator
» Domain tasting company have bought the domain in the front of your nose, how to prevent and react

Smarty, PHP

Using Smarty PHP template engine

2008-01-24 | Comment?

I believe that everyone have started to use PHP by adding some dynamic content to static HTML pages. Usually you would just add <?php ?> inside your HTML code. But over the time, if you add more and more PHP code, database support, etc. your scripts might become really messy, which is not good. Also, over the time PHP evolved from Hypertext Preprocessor in serious programming language which caused a different approach to PHP sites/applications. It is a bad practice these days to mix your PHP and HTML, logic and presentation. Instead you should try to distinguish your PHP and logic from the presentation (HTML and PHP).

You can use PHP for that as well. Separate your logic and presenation in two groups of PHP scripts. You would do all calculation and data preparation in the first group of scripts and then you will present the data with other group of scripts which would usually contain HTML code with the addition of dynamically generated content by printing it with <?php echo $my_var;?>. But if you have ever edited Word Press theme, you will see that HTML code becomes unreadable with the heavy usage of the <?php ?> elements inside.

That is where Smarty comes. You can write very clean HTML templates by using Smarty variables and Smarty commands. Smarty will also make the intermediate PHP code from your template that looks alike Word Press themes I have just mentioned, but you don’t have to worry about that. All you have to do is to make the pure HTML templates. Intermediate PHP code will be cached so your performances won’t be decreased.

Smarty is very simple tool. You create the object, assign values to Smarty variables and on the end of the script, you will call the display method to publish the page.

$smarty->assign('title', 'Hello, World');
$smarty->assign('message', 'some text');
$smarty->display('welcome.tpl');

Why I really like Smarty is that you can easily present the data in many different formats. For example, I use the same code base to retrieve data form the database and prepare objects and then use the different smarty templates to publish content for the different platforms.

Using Smarty

Lets take a look on the diagram above. Various scripts use my base classes to retrieve data from the databases. Once formated and prepared for publishing, by using different Smarty templates, the same data sets can be used to produce various content like web page, rss feed, content for mobile devices or even FBML for Facebook application.

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« eAccelerator
» Domain tasting company have bought the domain in the front of your nose, how to prevent and react