in reply to Re^2: Template modules, logic, and SOC
in thread Template modules, logic, and SOC

Sorry, this is just another pipeline module in my opinion. Callback means that the template is in charge of the program flow. In your case, the perl code calls the template and passes in data, thus pipeline.

Frankly, there's nothing new under the sun with templating. What you've done is pretty much identical to several modules that have been on CPAN for years, like CGI::FastTemplate. In fact, there's no reason you can't use HTML::Template, Text::Template, TT2, etc. in the way you're showing here.

I think you'll find that substitutions are really not enough. You at least need conditionals, because some things are too small to make a whole separate template and put the conditional in the perl code. And loops make for much more readable HTML code, which is one of the major reasons to use a template. And whitespace control is important. And formatting all of your numbers and dates in perl before you call the template gets ugly when you start needing the same data in multiple formats. And putting all this into your perl code means any significant HTML change has to be done by a perl programmer, which is fine for some dev teams and not for others.

My advice is to get over your idea that the view shouldn't contain logic. It's supposed to contain logic -- logic about how to display the data. It may sometimes be convenient to write the view logic in perl, and sometimes in a view-specific shorthand like TT2.

Replies are listed 'Best First'.
Re^4: Template modules, logic, and SOC
by arbingersys (Pilgrim) on Jan 23, 2008 at 20:48 UTC

    Frankly, there's nothing new under the sun with templating. What you've done is pretty much identical to several modules that have been on CPAN for years, like CGI::FastTemplate.

    Actually, I was aware that CGI::FastTemplate existed, and it was the only one I could see that worked "identically" to Template::Recall when I submitted to CPAN. It just seemed that CGI::FastTemplate existed in the wrong namespace for a general purpose (perhaps non- web-based) template system, and besides, it hasn't been updated since 1999.

    In fact, there's no reason you can't use HTML::Template, Text::Template, TT2, etc. in the way you're showing here

    Sure. All template systems essentially share the same goal. But there is one definite benefit that I can think of for using Template::Recall over these, at least for simple loops. I mention this in my introduction, regarding an output of row data to a simple table like that in my writeup. Using a pipeline system, you have to iterate over the data twice to output the table. Once when generating the array of hashes, and then again as (say) HTML::Template renders it against <TMPL_LOOP> on the call to print $template->output();. With Template::Recall (and Mason/PHP/JSP), there is no such cost, because you can output the row as soon as you receive it. Template::Recall just does it through a method call, which in my opinion, is cleaner.

    My advice is to get over your idea that the view shouldn't contain logic. It's supposed to contain logic -- logic about how to display the data. It may sometimes be convenient to write the view logic in perl, and sometimes in a view-specific shorthand like TT2.

    Actually, I've never held the opinion that the view should contain no logic. Substitution in my mind is effectively a part of logic. I just want to put as little logic as possible in the template. You're right, sometimes it may be better to have logic in the templates, for instance where you have a team of advanced designers, but there are plenty of other times where projects are driven by people who prefer to do as much in Perl as possible.