in reply to Simplifying repetitive flow structures

Ick. That's really hard to read through. Everything pretty much looks the same. I also find that using CGI's methods to generate HTML to create messy code and prefer to use a templating module such as HTML::Template. A template something like this should do the trick:
<TMPL_LOOP PHONEBOOK> <tr> <td> <TMPL_IF PIC> <TMPL_IF TYPE_OTHER> <img src="/images/thumb_<TMPL_VAR NAME=PIC>" </TMPL_IF> <a href="<TMPL_VAR NAME=PIC"><TMPL_VAR NAME=NAME></a> <TMPL_ELSE> <TMPL_VAR NAME=NAME> </TMPL_IF> </td> <td><TMPL_VAR NAME=PHONE></td> <td><TMPL_VAR NAME=FAX></td> <td><TMPL_VAR NAME=LOC></td> <td> <TMPL_IF TYPE_C> <TMPL_VAR NAME=CON> <TMPL_ELSE> <a href="mailto:<TMPL_VAR NAME=EMAIL>"><TMPL_VAR NAME=EMAI +L></a> </TMPL_IF> </td> </tr> </TMPL_LOOP>
Then all you have to do is create an LoH (list of hashes) that has your data plus TYPE_C and TYPE_OTHER set and use it like:
my $t = HTML::Template->new; $t->param(PHONEBOOK => \@data); print $t->output;
This has the advantage of seperating out your code and design and allowing somebody else to tweak the output. For more info on HTML::Template see its documentation or using CGI, DBI and HTML::Template (a mini tutorial with example code).

Hope this helps.

gav^

Replies are listed 'Best First'.
Re: Re: Simplifying repetitive flow structures
by Dogma (Pilgrim) on Apr 01, 2002 at 03:21 UTC
    That code does look pretty terrible when it's line wrapped. I'm not using HTML::Template for alot of reasons - mostly because this is running under mason/mod_perl and I'm trying to limit the number of differnt things to maintain. Code/html seperation with Mason is something I've considered. However your example doesn't simplify the problem. I still have to deal with variable numbers of columns with differing names depending on the value of "$type" and in addition to that the person may or maynot have an image in the directory as well (if 'pic' is defined or not).
      HTML::Template works great under mod_perl. Look in the FAQ section of the docs and set cache/shared_cache in the call to HTML::Template->new. If HTML::Template doesn't cut it then Template Toolkit (or here) is more powerful has support for expressions amongst things. Code/HTML seperation for me is not just a "good thing" it is a business critical thing. Generating HTML with CGI.pm just doesn't cut it in 2002.

      I gave a answer for the question you asked. I can only work with the information you gave, and I thought I covered all the bases. Maybe you need to re-think the design? Poor implementations are normally a symptom of bad design.

      gav^

        We're currently using the HTML::Template caching under Fast CGI and it works great. You read all the templates in when the server starts and it makes everything a little snappier.

        -biz-