I'll be honest. I've never really been a big fan of the template systems available to Perl (although I can't think of another language that comes even close to providing the options and maturity that Perl offers). Mason reminds me too much of my days writing ASP, with HTML and code strewn together, and it's always bothered me that TT2 and HTML::Template, although much cleaner, contain logic.
So I decided there really was More Than One Way To Do Things, and wrote a module called Template::Recall (available on CPAN). My idea was that you could get truer SOC if you kept the templates physically separate from the code and broke them into "callable" sections, because all you'd ever have to do to process a template was substitution.
For example, if you had the following three sections (header,row,footer),
a Template::Recall object ($tr) would render the row, on each iteration, something like[=== header ===] <table> [=== row ===] <tr><td>['product']</td><td>['quantity']</td><td>$['price']</td></tr> [=== footer ===] </table>
print $tr->render( 'row', { product=>$prod, quantity=>$quant, price=>$price } );
Consider that with HTML::Template you would render the product data to an array containing hashes, and then pass it to a <TMPL_LOOP> "logic" construct in the template. (To be iterated over again for output, I might add.)
A more detailed introduction to Template::Recall is at the following link, and the nuances may be made more apparent there.
http://blog.arbingersys.com/articles/TemplateRecall/article.htmlRecently, I came across this interesting post by Fred Moyer, called "Down with templating languages".
http://use.perl.org/~Phred/journal/33451He makes the following statement:
So here I present to you Moyer's Eleventh Rule of Templating Languages. "Any sufficiently complicated templating language contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Perl"
This is referring to how logic creeps in (Mason) or is built-in (TT2, HTML::Template) to the templates, making them a "language" all their own.
After considering, I think that Template::Recall is, by design, "11th law"-proof, because it only allows substitution. Perhaps it just seems that way because it is not (yet) a "sufficiently complicated" template language. I suspect this is not the case, but I'd like to hear the Monks' perspectives.
I consciously wanted all logic (presentation or otherwise) to be forced into the code. If you need to provide nested output, it would be done like
for (@groups) { # ... manipulate data ... $tr->render('group', \%group_data); for(@details) { # ... manipulate ... $tr->render('details', \%detail_data); } }
Includes would be done using multiple Template::Recall objects, e.g.
if ($department eq 'Technology') { print $tr->render( 'include_header', { header => $tr2->render('tech_dept') } ); } else { print $tr->render( 'include_header', { header => $tr2->render('default') } ); }
And as you can see, conditionals are also where, I feel, all logic should be: in the code.
So that's my perspective. I'm interested in hearing what the Monks have to say about it.
P.S. I don't think you can really know how an idea will be in implementation, until, well, you implement it. I've used Template::Recall with CGI::Application to build Sylbi, a true MVC application, and the templates have worked out pretty much how I expected. To me (naturally) the code feels very "clean".
http://sylbi.arbingersys.comThe msite/Sylbi/*.pm files in the Sylbi code can be perused for a taste of Template::Recall in use rendering not only HTML output, but also Perl code for eval.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Template modules, logic, and SOC
by Joost (Canon) on Jan 23, 2008 at 19:21 UTC | |
by kyle (Abbot) on Jan 23, 2008 at 19:34 UTC | |
by Joost (Canon) on Jan 23, 2008 at 19:50 UTC | |
by arbingersys (Pilgrim) on Jan 23, 2008 at 19:45 UTC | |
by perrin (Chancellor) on Jan 23, 2008 at 20:06 UTC | |
by arbingersys (Pilgrim) on Jan 23, 2008 at 20:48 UTC | |
by Joost (Canon) on Jan 23, 2008 at 19:58 UTC | |
by arbingersys (Pilgrim) on Jan 23, 2008 at 21:01 UTC |