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

I wrote a conversion utility the other day for a colleague. It takes an Excel spreadsheet (top forty music chart) and transforms it into a webpage.

My colleague was very happy, and I then I showed her the template. "It uses a very simple template language..." I said, and saw the look on her face. She does not want to learn another template language.

So, here's the thing. In HTML::Template you do it one of two different ways. You either have code like this:

<tmpl_loop name="chart"> <tr> <td><tmpl_var name="chartposition"></td> <td><tmpl_var name="artist"></td> <td><tmpl_var name="song"></td> </tr> </tmpl_loop>

or you have code like this:

<!--tmpl_loop name="chart" --> <tr> <td><!-- tmpl_var name="chartposition" --></td> <td><!-- tmpl_var name="artist" --></td> <td><!-- tmpl_var name="song" --></td> </tr> <!--/tmpl_loop -->

but in neither case do you have actual working HTML. It's not valid in the first case, and the second case, while valid, doesn't render anything in the browser.

What my friend the busy web-developer would certainly prefer is if she could code up a web page like this:

<tr (repeat this row for each song please)> <td>number</td> <td>band name</td> <td>song name</td> </tr>

But obviously, that isn't valid HTML exactly either. So, I had what's either a brainwave or it's not, but everyone in these days of HTML 4 is already adding arbitrary markers to their HTML anyway. The classes (or IDs).

My friend will have to add a class here or there anyway, just to get the CSS right, and it really doesn't much matter what they're called.

So why can't we have a version of HTML::Template which works something like this?

<tr class="tmpl-loop-chart"> <td class="tmpl-var-chartposition">1</td> <td class="tmpl-var-bandname">OUTKAST</td> <td class="tmpl-var-songname">Roses</td> </tr>

That is, one where the developer could make up a template with any old content in it they wanted, and the module would parse the HTML and figure out that the loop ended with the next (balanced) </tr> tag, the TDs close with the next (balanced) </td> tag and so on, and replace the content of that tag with the data, rather than the artificial template-language tag which the developer has to learn.

What do fellow monks think?



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Thinking Aloud About HTML::Template
by adrianh (Chancellor) on Jul 31, 2004 at 13:39 UTC

    You might want tot take a gander at Petal, which does pretty much what you asking for.

Re: Thinking Aloud About HTML::Template
by Joost (Canon) on Jul 31, 2004 at 12:21 UTC
Re: Thinking Aloud About HTML::Template
by dws (Chancellor) on Jul 31, 2004 at 17:16 UTC

    What do fellow monks think?

    I wrestled with this problem a while back, and developed a mildly convoluted scheme that allows templates to be viewed as HTML without losing their template-ness. See Browser-viewable HTML::Template templates.

    I've since headed the opposite direction--towards a more componentized view of the world--and now do things like:

    <TMPL_INCLUDE ESCAPE=0 table>
    where table is the result of expanding a separate template that knows how to generate HTML tables.

      Hi there, dws.

      Could you expand on this a bit? Are you talking about table templates that are loops-within-loops to generate your "tadahs" with in your "tahrahs" ? (that is to say, td's within tr's).

      I've done that before with templates, but at that point the html'ness is pretty degraded. Not to mention with a really fat table it's not very efficient (with HTML::Template).

      Anyway, I'd appreciate some more detail, if you have the time.

      Cheers,
      Matt

Re: Thinking Aloud About HTML::Template
by bradcathey (Prior) on Aug 01, 2004 at 10:19 UTC
    I'm a heavy user of HTML::Template and have entertained some of the same concerns about validation as you. However, it is only as a purist that I want it to validate before the page is rendered in the browser. Of course, when the page is actually visited, all H::T coding is gone, and is basically valid at that point. So, my question: is it worth reworking H::T, or even switching to a new system, just to satisfy my purist leanings? In an ideal world, maybe 'yes', but in a more practical one, I have to be profitable.

    Anyway, great mediation—one that I have not thought that thoroughly about.

    —Brad
    "Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton
      Thanks everyone for your thoughts and suggestions.

      I took a look at Petal, which I don't think exactly meets the brief as it uses non-valid attributes, but Seamstress (a word I haven't heard since "Tiny Dancer" incidentally) would appear to be roughly what I had in mind.



      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
      =~y~b-v~a-z~s; print
        I took a look at Petal, which I don't think exactly meets the brief as it uses non-valid attributes

        What's the problem with non-valid attributes? A web browser that doesn't recognise an attribute will ignore it - exactly what you want. The great thing about Petal is that you can put realistic dummy content within the tags in your template. When Petal runs, it replaces the content from the template with the data from your script. But if you view the template directly in a browser or a WYSIWIG HTML authoring tool, the dummy content will be displayed instead.