Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Can HTML::Template do this?

by Cody Pendant (Prior)
on Apr 26, 2004 at 22:56 UTC ( #348349=perlquestion: print w/replies, xml ) Need Help??

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

I produced a number of web "image gallery" pages, but I didn't use HTML::Template. Mea Culpa.

Having got that out of the way, here's what I had to do.

I produced tables in the following form, from either odd- or even-numbered lists of images:

-------- -------- | | | | | 1 | | 2 | | | | | | | | | -------- -------- -------- -------- | | | | | 3 | | 4 | | | | | | | | | -------- --------
but sometimes, if there were an odd number of images:
-------- -------- | | | | | 1 | | 2 | | | | | | | | | -------- -------- -------- | | | 3 | empty | | cell | | --------

So how exactly *would* I do that with HTML template, next time around?

My method this way was to have an odd-numbered cell chunk of code, which started a TR, and an even-numbered cell which ended one, and test for "having run out of content on an odd number" and dump in the blank cell chunk of code if necessary.

I'm scratching my head over the HTML::Template doco and trying to figure out how I would have done this -- any ideas?

=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Can HTML::Template do this?
by bmann (Priest) on Apr 26, 2004 at 23:19 UTC
    Sure. Turn on loop_context_vars and try this.

    <tmpl_if name="__last__"> <tmpl_if name="__odd__"> <td>Empty cell here.</td> </tmpl_if> </tmpl_if>

      What a great feature, I should have looked harder, shouldn't I? Thanks a lot.

      =~y~b-v~a-z~s; print
Re: Can HTML::Template do this?
by dragonchild (Archbishop) on Apr 26, 2004 at 22:59 UTC
    Have a first and a second and check to see if it exists. I just coded this up today in a results table.
    <TMPL_LOOP NAME="RESULTS"> <tr> <td> <TMPL_IF NAME="FIRST"> <TMPL_VAR NAME="FIRST"> <TMPL_ELSE> &nbsp; </TMPL_IF> </td> </tr> <tr> <td> <TMPL_IF NAME="SECOND"> <TMPL_VAR NAME="SECOND"> <TMPL_ELSE> &nbsp; </TMPL_IF> </td> </tr> </TMPL_LOOP>

    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Can HTML::Template do this?
by ph0enix (Friar) on Apr 27, 2004 at 08:22 UTC

    You can use DEFAULT param (new in HTML::Template version 2.6) of TMPL_VAR tag

Re: Can HTML::Template do this?
by thraxil (Prior) on Apr 27, 2004 at 15:34 UTC

    i've stopped using tables for image galleries altogether. instead, i prefer to just spit out a bunch of divs and float them with CSS. eg, see this gallery. the advantages are that it is simple to generate (you can just use a plain <tmpl_loop>) and if you resize the browser window, they will reflow to fit. this allows the user to take full advantage of a big screen without making users with small screens scroll horizontally. if you still want to limit it to a certain number of columns, you just enclose the whole thing in a div with a fixed width. if you have images with irregular dimensions, you need to force the enclosing divs out to the maximum width and heights to make it look neat.

    i used to try to do these with tables and it involved all kinds of annoying code to add in empty entries for the remaining table cells.

Re: Can HTML::Template do this?
by inman (Curate) on Apr 27, 2004 at 10:23 UTC
    Just go for it anyway. The browser will finish your table for you leaving blank space where there are no data elements. This saves you from worrying too much about counting cells etc. You are then able to create tables with any number of columns.

    The table below shows this.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://348349]
Approved by DigitalKitty
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2023-10-02 12:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found