Making Smarty EZier.
If you have used Smarty for templating your websites then you already know what a great resource it is. If not, you are missing out. Go to Smarty.php.net and check it out.
In this article I will explain how using Smarty with EzSQL by Justin Vincent (justinvincent.com) can make your life even “ezier”. My intent here is not to explain the in depth workings of Smarty or EzSQL but to show how the use of these two classes together is synergistic.
First we’ll have quick look at EzSQL, then Smarty, then the two combined.
EzSQL:
When getting data from a database using native php it might look something like this:

`mysql_connect("localhost", "mysql_user", "mysql_password") or die("could not connect"); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result)) { printf ("ID: %s Name: %s", $row[0], $row["name"]); } mysql_free_result($result);`

In the reality I think many of us now use a class of some kind so it would look a little more like this (Y.M.M.V.)

`require '/path/to/myConnector.php'; $db=new myConnector("localhost", "mysql_user", "mysql_password"); $db->query("SELECT id, name FROM mytable"); while ($db‡next_record()){ printf ("ID: %s Name: %s", $db->f(‘id’), $db->f(‘name’); }`

I think you’d agree that’s fewer lines and generally a better solution. Using a database class is great as it wraps the database, makes getting the data easier, but doesn’t cover the presentation aspect. That still has to be done by intermingling php and HTML
EzSQL is only a little different in it’s set up, however the results are returned as an array as you can see here.

`define("EZSQL_DB_USER", "mysql_user"); define("EZSQL_DB_PASSWORD", "mysql_password"); define("EZSQL_DB_NAME", "my_db"); define("EZSQL_DB_HOST", "localhost"); require ‘/path/to/ez_sql.php'; $result_array = $db->get_results("SELECT id, name FROM mytable"); foreach($result_array as $row_obj) { printf ("ID: %s Name: %s", $db->id, $db->name; }`

Smarty:
Next we’ll take a look at the Smarty process
Smarty is a class. In simplistic terms it's usage is:
- Instantiate a Smarty object
- Push the data for the page into the Smarty object
- Get Smarty to apply the template(s) to the data -- (“skin” the object so to speak)
In code it looks like this:

`include '/path/to/Smarty.php'; $Smarty=new Smarty; $Smarty->assign('somevar', 'some data I want to display'); $Smarty->assign('some_db_results', $db->get_my_data()); $Smarty->display('name_of_template.tpl');`

The template had entries for {$somevar} and {$some_db_results} so the assigned data is displayed inside the template at those points.
You have probably already figured out the ending to this story but in case you haven’t, this is what happens when you put these two classes together.
Putting them together:
This is the code for both the php file and the template file. The synergy being that the results from EzSQL can be passed straight into Smarty and the layout is done there. This means less coding for the programmer and more flexibility for the designer.

`define("EZSQL_DB_USER", "mysql_user"); define("EZSQL_DB_PASSWORD", "mysql_password"); define("EZSQL_DB_NAME", "my_db"); define("EZSQL_DB_HOST", "localhost"); require ‘/path/to/ez_sql.php'; // the $db object is instantiated by the php file include '/path/to/Smarty.php'; $Smarty=new Smarty; $Smarty->assign('DB_RESULTS', $db->get_results("SELECT id, name FROM mytable"); $Smarty->display('template.tpl'); //template file template.tpl <table border="0" cellspacing="0" cellpadding="3"> {foreach from=$DB_RESULTS item="row_obj"} <tr> <td>ID: {$row_obj ->id}</td> <td>Name: {$row_obj ->name}</td> </tr> {/foreach} </table>`

Of course this is not a real world example. In the real world, at least in my real world, all the configuration is done in a “loader” file that takes care of all the constant definitions, data paths, instantiations and so on. This file is prepended in the httpd container for the domain or in .htaccess file so the process is automated. So, in reality the php file only contains the last two lines of the example.
Since switching to this method of creating sites my workload has gotten lighter, my code is more readable and the number of line of code is far less. Even the design side is more fun as you can control the display in the presentation layer and not have to worry abobut tweaking the underlying PHP files. All in all faster and “ezier”... Try it.
Happy coding,
Steve Warwick Ph.D.
articles@clickbuild.com

For information on using my modified version of Justin’s class ez_results with Smarty check out my article “EZ pagination For Smarty.”