----- Original Message -----From: Bob DissSent: Wednesday, October 06, 2004 10:31 PMSubject: [htmltmpl] MARK= enhancement to the HTML::Template LOOP constructGreetings 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=some-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=3" 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=-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=+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=TOTGRP/GRP", as in "MARK=3/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="TOP"><TD><TMPL_LOOP NAME="foo" MARK="3/1"><TMPL_IF __MARK__> output an item in column 1<BR></TMPL_IF></TMPL_LOOP></TD><TD><TMPL_LOOP NAME="foo" MARK="3/2"><TMPL_IF __MARK__> output an item in column 2 <BR></TMPL_IF></TMPL_LOOP></TD><TD><TMPL_LOOP NAME="foo" MARK="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!