DamnDirtyApe has asked for the wisdom of the Perl Monks concerning the following question:

Over the past couple of months, I've become acutely aware of the value of separating presentation logic from application logic. A project I am currently working on in that other web scripting language (you know, the one with Poor Hash Practices ;-) has no such separation, and becomes less maintainable and more of a jumbled mess of code with each new page. I find myself turning to the Template Toolkit for more and more of my dynamic generation needs.

Lately, though, I've been having a dilemma as to the style which my templates take. Anyone who spends any time in the Monastery knows that is one of our oldest and most sacred incantations. So sacred, in fact, that seldom does a hand go unslapped for statements like: print "<H1 STYLE=\"color: blue\">Hi, $attrib->{name}</H1>" ; that could be replaced with: print $q->h1( { -style => 'color: blue' }, 'Hi,', $attrib->{name} ) ;

The pros of using the latter method are evident on so many levels. First, the code is easier on the eyes. Personally, I cringe at having to escape quotes -- readability takes a sharp nosedive when this happens. Second, Perl will actually prevent you from generating broken HTML, and let you know when you try to. IMO, and the O of many, many others, It's just simply the way to go.

My question, in a nutshell, is this: how strongly do people feel about the use of CGI from within Template Toolkit?

My first Template Toolkit scripts used the CGI plugin. I found that, in some cases, it seemed to be serious overkill, especially for static elements. For example, a simple table with a page title and an image using CGI:

[% mycgi.table( { border => '0' }, Tr( td( [ h1( 'My Page' ), img( { src => 'mypage.png' } ) ] ) ) ) %]
And without:
<table border="0"> <tr> <td> <h1>My Page</h1> </td> <td> <img src="mypage.png" /> </td> </tr> </table>

Is it just me, or is the latter example easier to visualize?

Another advantage to the second method is that I don't need to generate something from a template to see how it looks -- I can simply load my template in a web browser. This doesn't give you the full effect of your template (eg. FOREACH loops), but overall I find it extremely helpful. When you use the CGI plugin, it seems to me that you lose some of this ability.

I'd really appreciate getting some feedback about the methodology people use when using Template Toolkit on small-to-medium-scale web development projects. Does the edict of use CGI carry over just as strongly to templates, or do most people use handwritten HTML instead? Is it considered poor taste to mix HTML with CGI plugin code?

As a small aside, I'm sure the HTML::Template camp is laughing at this, as they never have to deal with it.

Much thanks for your boundless knowledge and wisdom.

D a m n D i r t y A p e
Home Node | Email