Skip to content
This repository
tree: 9f755d05c0
jv2222 February 02, 2012
executable file 185 lines (132 sloc) 7.507 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<title>Making Smarty EZier.</title>
</head>

<body bgcolor="#ffffff">
<b>Making Smarty EZier.<br>
</b><br>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.<br>
<br>
In this article I will explain how using Smarty with EzSQL by Justin Vincent (<a href="http://justinvincent.com/">justinvincent.com</a>) can make your life even &#147;ezier&#148;. 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.<br>
<br>
First we&#146;ll have quick look at EzSQL, then Smarty, then the two combined.<br>
<br>
<b>EzSQL:<br>
</b><br>
When getting data from a database using native php it might look something like this:<br>
<br>

<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>mysql_connect(&quot;localhost&quot;, &quot;mysql_user&quot;, &quot;mysql_password&quot;)<br>
or die(&quot;could not connect&quot;);<br>
mysql_select_db(&quot;mydb&quot;); <br>
$result = mysql_query(&quot;SELECT id, name FROM mytable&quot;); <br>
while ($row = mysql_fetch_array($result)) <br>
{ <br>&nbsp;&nbsp;printf (&quot;ID: %s Name: %s&quot;, $row[0], $row[&quot;name&quot;]);<br>
} <br>
mysql_free_result($result); <br>
</code></td>
</tr>
</table>
<br>
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.)<br>
<br>

<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>require '/path/to/myConnector.php';<br>

$db=new myConnector(&quot;localhost&quot;, &quot;mysql_user&quot;, &quot;mysql_password&quot;);<br>
<br>

$db-&gt;query(&quot;SELECT id, name FROM mytable&quot;);<br>

while ($db&#135;next_record()){<br>

&nbsp;&nbsp;printf (&quot;ID: %s Name: %s&quot;, $db-&gt;f(&#145;id&#146;), $db-&gt;f(&#145;name&#146;);<br>

}<br>
</code></td>
</tr>
</table>
<br>
I think you&#146;d agree that&#146;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&#146;t cover the presentation aspect. That still has to be done by intermingling php and HTML<br>
<br>
EzSQL is only a little different in it&#146;s set up, however the results are returned as an array as you can see here.<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>define(&quot;EZSQL_DB_USER&quot;, &quot;mysql_user&quot;); <br>
define(&quot;EZSQL_DB_PASSWORD&quot;, &quot;mysql_password&quot;);<br>


define(&quot;EZSQL_DB_NAME&quot;, &quot;my_db&quot;);<br>


define(&quot;EZSQL_DB_HOST&quot;, &quot;localhost&quot;); <br>


require &#145;/path/to/ez_sql.php';<br>
<br>


$result_array = $db-&gt;get_results(&quot;SELECT id, name FROM mytable&quot;);<br>


foreach($result_array as $row_obj) {<br>
&nbsp;&nbsp;printf (&quot;ID: %s Name: %s&quot;, $db-&gt;id, $db-&gt;name;<br>


}<br>
</code></td>
</tr>
</table>
<br>
<br>
<b>

Smarty: <br>
</b><br>


Next we&#146;ll take a look at the Smarty process<br>
<br>


Smarty is a class. In simplistic terms it's usage is: <br>


- Instantiate a Smarty object<br>


- Push the data for the page into the Smarty object<br>


- Get Smarty to apply the template(s) to the data -- (&#147;skin&#148; the object so to speak)<br>
<br>


In code it looks like this:<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>include '/path/to/Smarty.php';<br>$Smarty=new Smarty;<br>$Smarty-&gt;assign('somevar', 'some data I want to display');<br>$Smarty-&gt;assign('some_db_results', $db-&gt;get_my_data());<br>$Smarty-&gt;display('name_of_template.tpl');<br>
</code></td>
</tr>
</table>
<br>


The template had entries for {$somevar} and {$some_db_results} so the assigned data is displayed inside the template at those points.<br>
<br>


You have probably already figured out the ending to this story but in case you haven&#146;t, this is what happens when you put these two classes together.<br>
<br>
<b>Putting them together:<br>
<br>
</b>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.<br>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#f5f5dc">
<tr>
<td><code>define(&quot;EZSQL_DB_USER&quot;, &quot;mysql_user&quot;); <br>
define(&quot;EZSQL_DB_PASSWORD&quot;, &quot;mysql_password&quot;);<br>


define(&quot;EZSQL_DB_NAME&quot;, &quot;my_db&quot;);<br>


define(&quot;EZSQL_DB_HOST&quot;, &quot;localhost&quot;); <br>
require &#145;/path/to/ez_sql.php'; <br>
// the $db object is instantiated by the php file<br>
<br>include '/path/to/Smarty.php';<br>$Smarty=new Smarty;<br>
<br>$Smarty-&gt;assign('DB_RESULTS', $db-&gt;get_results(&quot;SELECT id, name FROM mytable&quot;);<br>$Smarty-&gt;display('template.tpl');<br>
<br>
//template file template.tpl<br>
<br>


<FONT face="Courier New">&lt;table border="0" cellspacing="0" cellpadding="3"&gt;<BR>
{foreach from=$DB_RESULTS <FONT
color=#ff0000>item="row_obj"</FONT>}<BR>
&nbsp;&lt;tr&gt;<BR>
&nbsp;&nbsp;&lt;td&gt;ID: {$row_obj -&gt;id}&lt;/td&gt; <BR>
&nbsp;&nbsp;&lt;td&gt;Name: {$row_obj -&gt;name}&lt;/td&gt;<BR>
&nbsp;&lt;/tr&gt;<BR>
{/foreach}<BR>
&lt;/table&gt;</FONT><br>
</code></td>
</tr>
</table>
<br>


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 &#147;loader&#148; 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.<br>
<br>
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 &#147;ezier&#148;... Try it.<br>
<br>
Happy coding,<br>
<br>

Steve Warwick Ph.D.<br>


articles@clickbuild.com<br>
<hr>


For information on using my modified version of Justin&#146;s class ez_results with Smarty check out my article &#147;EZ pagination For Smarty.&#148;<br>
</body>

</html>
Something went wrong with that request. Please try again.