Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Trying to understand template-foo

by perrin (Chancellor)
on Sep 12, 2003 at 04:00 UTC ( [id://290914]=note: print w/replies, xml ) Need Help??


in reply to Trying to understand template-foo

Hi, I think I talked to you on the Class::DBI mailing list. I just wrote up a big long response and then lost it when I accidentally closed the browser. I don't have the patience to type it all in again, but here are the major points:

  • Read some articles about using the model-view-controller pattern with Perl. You can find some on here, and there are some on perl.com, including one that I wrote.
  • Imagine that your code has to produce a data structure with zero HTML in it so that it can be used on a dozen different sites with different layouts, including one that uses PDF and one that is plain text e-mail.
  • In yur example, I think you'll have a list of countries each of which contains a list of regions, each of which contains a list of wines. Each country, region, and wine will end up being a hash. Those hashes could potentially be represented as objects, and you could make Class::DBI classes for them, although in your example you have used a single large query instead of traversing object relationships to get the data.
  • Figure out how to make your code produce that data structure instead of producing HTML.
  • Write a template that takes that data structure and produces the HTML you want.
  • You want the template to be as simple and as dumb as possible.
To answer your question about CSS, it's mostly irrelevant. The templates can contain any HTML you want including CSS. The important thing is that they only get to look at the data, not change any of it.

I hope that gets you started. If you come to a New York Perl Mongers meeting I can show you how to do it in detail.

Replies are listed 'Best First'.
Re: Re: Trying to understand template-foo
by jest (Pilgrim) on Sep 16, 2003 at 17:58 UTC

    Thanks to all who replied; I've been considering everyone's comments and it's all been helpful. Which isn't to say that I now know exactly what I'm doing....

    For now I followed mostly Perrin's advice, and rewrote the included code to instead generate a complex data structure that I passed to a Template Toolkit template. I kept all the summary calculations in the Perl, so the only programming-ish things in the template were FOREACH loops and a few conditionals to handle things like the alternate-colored lines and such.

    It works perfectly, i.e. exactly the same as it was before but more modifiable. I must say that I still feel a little uncomfortable with it--the programming in the template, while hardly complex, is still programming, and it's distracting to have a different way of handling looping, comparisons, and data structures. But I'm learning, and TT has so much neat stuff that I concede it's probably worthwhile to know more about it in the long run.

    Thanks again for the ideas. Next stop, on to Class::DBI!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://290914]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found