NYCPHP Meetup

NYPHP.org

[nycphp-talk] To Smarty Or Not to Smarty: That Is The Question

Mikko Rantalainen mikko.rantalainen at peda.net
Tue Sep 5 09:16:17 EDT 2006


Richard Harding wrote:
> Mikko Rantalainen wrote:
>>
>> The reason I think that one shouldn't need a template system is that 
>> (X)HTML is supposed to be only about the structure and semantics. If 
>> you don't whip up a <table> element for layout the UI guys don't 
>> need to modify the (X)HTML source. They just touch the CSS file.
> 
> Personally, I like the template system approach. I have builders that 
> perform my logic. They use my objects to fetch the list of user accounts 
> from the database, for instance. Then I assign that list to the template 
> system. The data is now available for output. I can then create numerous 
> templates that use that same data for various types of output. One 
> builder might power 5 different template views and I only have to update 
> that one builder if some logic changes and all 5 templates are up to 

I see. I guess that works well, if it's okay to fetch all the data 
from database and display only some of it. I'd be a little afraid 
that in large scale that puts quite some unnecessary extra work for 
the both database server and the PHP engine.

> date. I don't so how you go about that type of flexibility without a 
> template system of some sort. Care to share some info on how you use 
> straight style sheets to get your output going?

If you have user account info and you just want to display it, you 
should first think what kind of semantic structure does this data 
have? If you have a list of users each of which have multiple 
fields, then it sounds to me that your logical structure is a table.

So logically it's like this:

UID	Name	Foobar
1	John	"apple"
12	Mary	7

Semantic markup for this would look like

<table class="$class">
<tr><th>UID</th><th>Name</th><th>Foobar</th></tr>
<tr><td>1</td><td>John</td><td>"apple"</td></tr>
<tr><td>12</td><td>Mary</td><td>7</td></tr>
</table>

The only thing you need to change for different rendering of the 
above data is to set $class variable to another value. I don't 
understand how many different ways there could be to encode that 
same information in XHTML. Therefore, I don't understand the need 
for templating engine to generate/modify that part of the source. To 
me, the only question is what solution has the best performance? A 
template engine is seldom the correct answer.

Then you just write CSS file that specifies the rendering. It could 
be pretty much anything, except that with MSIE6/win32 you cannot 
say, for example, stuff like

table.special { display: block; }

because MSIE6/win32 will always behave as if an element called 
"table" had hardcoded CSS property "display: table".

That's where the problems start. That's the reason you might want to 
use a template engine. If you're not happy with the hardcoded 
rendering the MSIE6/win32 has for your markup you have to *change 
your semantic markup* to match the intended rendering instead of 
intended meaning! Another reason could be that your layout guys 
don't understand CSS and use HTML for layout instead.

I believe that to change the semantics of your data (the HTML 
markup) you *have* to modify the code/logic also. I don't see the 
advantage of separating those.

-- 
Mikko



More information about the talk mailing list