in reply to Re: Editing Entries In A Flat Database
in thread Editing Entries In A Flat Database with cgi

It seems to me that everytime someone advocates using CGI.pm for HTML generation, they give examples that show (IMHO) no advantage over printing HTML in a 'heredoc'.

I have found many cases where CGI.pm HTML functions are very useful (such as generating checkbox groups from data), but in cases like this, I find the HTML more readable.

Impossible Robot
  • Comment on Re: Re: Editing Entries In A Flat Database

Replies are listed 'Best First'.
Re: Re: Re: Editing Entries In A Flat Database
by IlyaM (Parson) on Dec 28, 2001 at 23:08 UTC
    IMHO there should be no HTML in Perl code at all. Neither in form of heredocs nor in form of CGI.pm method calls. Use templates!

    --
    Ilya Martynov (http://martynov.org/)

Re(3): Editing Entries In A Flat Database
by dmmiller2k (Chaplain) on Dec 28, 2001 at 20:58 UTC

    Perhaps you're right. That was a bad example. One way CGI.pm shows its usefulness which seems to been obscured here is with deeply nested constructs (table within a table within a form, etc.).

    By replacing the BEGIN/END (tag) paradigm with a nested function call paradigm, CGI all but eliminates the kind of unbalanced tag nuisances one frequently encounters when generating HTML dynamically.

    IMHO, I find looking at naked HTML (in a HEREDOC or otherwise) annoying for its waste of space (I suppose XML rubs me much the same way) and its near-redundancy; I dislike typing it even more.

    I suppose the advantage is better illustrated with this comparison:

    <FORM METHOD='POST> <INPUT TYPE="hidden" NAME="mode" VALUE="confirm"> <TABLE BORDER=0> <TR> <TD> <TABLE WIDTH='100%'> <TR> <TH>ColA</TH> <TH>ColB</TH> <TH>ColC</TH> </TR> <TR> <TD>Value A</TD> <TD>Value B</TD> <TD>Value C</TD> </TR> <TR> <TD><INPUT TYPE="submit"></TD> <TD><INPUT TYPE="reset"</TD> <TD>&nbsp;</TD> </TR> </TABLE> </TD> </TR> </TABLE> </FORM>

    Compared with:

    form( { -method => 'POST' }, hidden( -name => 'mode', -value => 'confirm' ), table( { -border => 0 }, Tr( td( table( { -width => '100%' }, Tr( th( [ 'ColA','ColB','ColC' ] ), td( [ 'Value A','Value B','Value C' +] ) td( [ submit, reset, '&nbsp;' ] )) ))) ) )

    Oh, you want to write all that to the browser?

    print form( { -method => 'POST' }, hidden( -name => 'mode', -value => 'confirm' ), table( { -border => 0 }, Tr( td( table( { -width => '100%' }, Tr( th( [ 'ColA','ColB','ColC' ] ), td( [ 'Value A','Value B','Value C' +] ) td( [ submit, reset, '&nbsp;' ] )) ))) ) )

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime
      For any complex website I would strongly disagree with the advice to use CGI's html generation methods. The more complex the overall site, the more I tend to disagree. A recent node with a good summary of why templating is a good idea was given by maverick at Re: Re: Re: Re: Code Critique. (There are plenty of others out there of course.)
        "For any complex website I would strongly disagree with the advice to use CGI's html generation methods."

        Depending upon the level of complexity, I would tend to agree with your assessment, although for my own work, I tend to mix and match. For instance, I might template the site look-and-feel components (top or left navbar, etc.) especially if I need a lot of Javascript to glue it all together, but for low- to medium-complexity screens, I sometimes choose to use CGI.pm exclusively for generating the HTML.

        I find that for highly dynamic screens, CGI.pm is easier to work with than, say Dreamweaver and the like. It rarely takes more than a couple of iterations to tweak it into shape.

        My .02.

        dmm

        You can give a man a fish and feed him for a day ...
        Or, you can
        teach him to fish and feed him for a lifetime