lazy saunter

(personal learnings in computation)

Wednesday, March 07, 2007

smarty (template engine), intro

preface: one goal of mine for this year was to get experience using a PHP template engine.

searching for a "better" way to program PHP websites, i came across the idea of using a "template engine" whose goal is to separate presentation code from application logic code. this appealed to me as the code in several of my projects became difficult to read due to the unaesthetic and seemingly unorganized mixture of html and php within the same file.

about a week ago i had reached a point on a project that i was happy with and did not seem to have many other projects coming in in the near future. i had written it using the approach i ve been using for a couple months now but decided to convert it with a "templated" approached.

research showed me that "Smarty" is one of the more popular template engines and in fact has a very developed website to support its users so i decided to start with it. here is a basic view of how it works:

instead of having a single "page.php", you would have a "page.tpl" which would contain something like:

html>
body>
your name is {$name}.
/body>
/html>

the actual "page.php" file would basically define the value of the variable "name":

?php
require 'Smarty.class.php';
$smarty = new Smarty;
$smarty->assign('name', 'huitzilopochtli');
$smarty->display('page.tpl');
?

the php file would be located in a web-accessible directory and would be the responsibility of the "programmer" on the team. the .tpl file would not be web accessible but would be in a "templates" directory which would be at the same level as a "templates_c" directory and maybe a "libs" directory where included files could be kept. this file would be the responsibility of the "web designer" on the team. the work of these two together would result in a functioning web page yet neither would need to know the details of the files of the other (besides the variables and values passed between the two).

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home