Re: [htmltmpl] MARK= enhancement to the HTML::Template LOOP construct
Brought to you by:
samtregar
From: Mathew R. <mat...@re...> - 2004-10-06 23:58:51
|
My local copy of H::T is modified so that a) you can create custom = TMPL_xxx tags, and b) you can create custom ESCAPE tags by overloading a = H::T::Escape (I have factured out that functionality into seperate = modules). I'd be interested to how you did this MARK stuff - I have previously = used H::T::Expr to generate custom layouts, although I consider this = solution to be rather hack'ish since I tend to think that TMPL_LOOP's = need not always loop from start to finish of Perl array. regards, Mathew ----- Original Message -----=20 From: Bob Diss=20 To: htm...@li...=20 Sent: Wednesday, October 06, 2004 10:31 PM Subject: [htmltmpl] MARK=3D enhancement to the HTML::Template LOOP = construct Greetings everyone! I've been using HTML::Template on my site for = several years now, and I'm really pleased with what it can do and how = easy it is to teach my HTML'ers. I'd like to share with you some = enhancements I've done to make it even more powerful (for me, at least). I make heavy use of the <TMPL_LOOP> tag on my site. However, I seem = to always want to do something that's hard. For example, I want to = layout the loop items in three columns (instead of two using the __ODD__ = tag). Or, I want to layout just the first 5 items and (if there are = more) make a link to "see more". Or, I want to lay items in 4 balanced = columns (i.e. A-G arranged vertically in the first column, H-N in the = second, O-U in the third, V-Z in the last). To accomplish this, I implemented a "MARK=3Dsome-value" tag for the = <TMPL_LOOP> construct. This MARK tag requests that the additional loop = context variables __MARK__, __MARKFIRST__, and __MARKLAST__ be set. = There are four possible formats for the MARK value. If I say "MARK=3D3" then the first item and every third item = thereafter will have __MARK__ set to true (i.e. it will be true for = items 1, 4, 7, 10, etc.). __MARKFIRST__ is set true whenever __MARK__ = is true, and __MARKLAST__ is set true for the last item before __MARK__ = is true (i.e. items 3, 6, 9, etc.). With this I can easily setup = N-column displays in HTML by triggering my <TR> starts and ends with = __MARKFIRST__ and __MARKLAST__. If I say "MARK=3D-6" then all the items before item 6 (items 1 through = 5) will be __MARK__'ed true. The first item will be __MARKFIRST__, and = the 5th item (or the last item if there are less than 6 items) will be = __MARKLAST__. This helps me process a list of items when I just want to = show the first N, but want to be smart enough to know if there are more = items to talk about. Since the <TMPL_LOOP> iterates over all the items, = I can trap the __LAST__ item and check to see if it was __MARK__'ed. If = so, I can offer a "more" link. Of course, there's a complimentary "MARK=3D+5" option to mark the = items that occur after item 5 (items 6 on). Finally, the hardest thing to do is layout balanced columns. To do = this I specify "MARK=3DTOTGRP/GRP", as in "MARK=3D3/2". This tells the = loop to divide the items in to TOTGRP balanced groupings (three in this = case), and __MARK__ the items in the GRP grouping (two in this case). = For example: I often have a list of alphabetized names, and I want to = show them in a way that people are used to, which is to have them = vertically alphabetized, and split into N balanced columns. Using this = tag my HTML becomes something like this: <TABLE><TR VALIGN=3D"TOP"> <TD> <TMPL_LOOP NAME=3D"foo" MARK=3D"3/1"> <TMPL_IF __MARK__> output an item in column 1<BR></TMPL_IF> </TMPL_LOOP> </TD>=20 <TD> <TMPL_LOOP NAME=3D"foo" MARK=3D"3/2"> <TMPL_IF __MARK__> output an item in column 2 <BR></TMPL_IF> </TMPL_LOOP> </TD>=20 <TD> <TMPL_LOOP NAME=3D"foo" MARK=3D"3/3"> <TMPL_IF __MARK__> output an item in column 3 <BR></TMPL_IF> </TMPL_LOOP> </TD> </TR></TABLE> I've found these enhancements to be very useful, as they save me from = coding layout decision flags in my loop items, while enabling more = sophisticated layout possibilities. Comments? Thoughts? Suggestions? It works for me -- would it work = for anyone else? -Bob Diss -------------------------------------------------------------------------= ----- Do you Yahoo!? vote.yahoo.com - Register online to vote today! |