Re: CGI.pm and HTML tables

From: Bill Karwin (bill_at_karwin.com)
Date: 12/18/04

  • Next message: John Bokma: "Re: CGI.pm and HTML tables"
    Date: Fri, 17 Dec 2004 18:54:06 -0800
    
    

    ladygrinningsoul wrote:
    > Would you happen to know
    > of samples that show a complex page layout using nested tables, cells, and
    > CGI.pm?

    IMHO, CGI.pm is not appropriate for this task. I have written pretty
    significant apps using CGI.pm's HTML shortcut functions, but I found
    that it makes it really difficult to do any redesign of the page layout.
      It's far easier to use a templating module such as HTML::Template or
    Template-Toolkit when you have complex HTML layout requirements.

    If you really need to use CGI.pm and nothing else, then you should read
    the section titled "THE DISTRIBUTIVE PROPERTY OF HTML SHORTCUTS" in the
    CGI.pm pod documentation. There's a simple example included:

    print table({-border=>undef},
        caption('When Should You Eat Your Vegetables?'),
        Tr({-align=>CENTER,-valign=>TOP},
        [
           th(['Vegetable', 'Breakfast','Lunch','Dinner']),
           td(['Tomatoes' , 'no', 'yes', 'yes']),
           td(['Broccoli' , 'no', 'no', 'yes']),
           td(['Onions' , 'yes','yes', 'yes'])
        ]
        )
    );

    But one can also replace any of those plain string elements in the lists
    of table cells with another table() function.

    print table({-border=>undef},
        caption('When Should You Eat Your Vegetables?'),
        Tr({-align=>CENTER,-valign=>TOP},
        [
           th(['Vegetable', 'Breakfast','Lunch','Dinner']),
           td(['Tomatoes' , 'no',
             table(
               Tr(
               [
                 td(['cold:','yes']),
                 td(['hot:','no'])
               ]
               )
             )
             ]
           ),
           td(['Broccoli' , 'no', 'no', 'yes']),
           td(['Onions' , 'yes','yes', 'yes'])
        ]
        )
    );

    This gets pretty mind-shattering pretty quickly. It's a real pain to
    construct anything like modern web pages with this method, and even more
    painful to try to debug or change the layout of an existing application.
      So I recommend using some template framework.

    For instance, I recommend Template Toolkit. You can probably install it
    as follows:
       cpan -i Template
    or the longer way:
       perl -MCPAN -e 'install Template'

    Here's a link to the tutorial for creating web pages with Template Toolkit:
    http://www.template-toolkit.org/docs/plain/Tutorial/Web.html

    For example, the web page from the CGI.pm doc could be designed using a
    template like this:

    <HTML>
    <TABLE BORDER=1>
    <TR ALIGN=CENTER VALIGN=TOP>
       [% FOREACH hdr = headers %]
       <TH>[% hdr.text %]</TH>
       [% END %]
    </TR>
    [% FOREACH row = rows %]
    <TR ALIGN=CENTER VALIGN=TOP>
       [% FOREACH cell = row.cells %]
       <TD>[% cell.text %]</TD>
       [% END %]
    </TR>
    [% END %]
    </TABLE>
    </HTML>

    And the CGI script then contains no details about the HTML presentation,
    only the data values to put in the dynamic portions of the page.

    #!/usr/bin/perl -w

    use strict;
    use Template;

    print "Content-type: text/html\n\n";
    my $t = Template->new();

    my $vars = {
       headers => [
         { text => 'Vegetable' },
         { text => 'Breakfast' },
         { text => 'Lunch' },
         { text => 'Dinner' },
       ],
       rows => [
         { cells => [
           { text => 'Tomatoes' },
           { text => 'no' },
           { text => 'yes' },
           { text => 'yes' },
         ] },
         { cells => [
           { text => 'Broccoli' },
           { text => 'no' },
           { text => 'no' },
           { text => 'yes' },
         ] },
         { cells => [
           { text => 'Onions' },
           { text => 'yes' },
           { text => 'yes' },
           { text => 'yes' },
         ] }
       ]
    };

    $t->process('tt.html', $vars);

    You will thank yourself in the long run, and so will the next person who
    inherits these CGI scripts from you!

    Regards,
    Bill K.


  • Next message: John Bokma: "Re: CGI.pm and HTML tables"

    Relevant Pages

    • Re: Advice on page layout and formatting
      ... Ron Symonds (Microsoft MVP - FrontPage) ... > proficient at HTML and have created a number of websites using my own HTML ... > Inside the blue outlines of the layout cells I created there are a number ...
      (microsoft.public.frontpage.client)
    • RE: Moving layout cells
      ... Tables are tables and cells are cells, whether they are used for layout or ... the difference with html is negligible. ... I'm guessing the layout CONTENT is the loading time problem - if the images ...
      (microsoft.public.frontpage.client)
    • Re: Improving page layout when doing elementary CGI programming.
      ... JSP/JSF/Java servlets is that Netbeans makes it trivially easy to ... really how HTML works: presumably NetBeans either has some standard ... the usual way of handling HTML layout in Perl ... pushing too much logic down into the template ...
      (comp.lang.perl.misc)
    • Re: is anyone here going to help me??? does anyone here know anything at all???
      ... I am very sure that if I post my broken template and they ... Until like three or four days ago, everything on my blog displayed ... The generic Google ad beneath the blog title box displayed perfectly. ... I tried to publish and got a broken HTML error. ...
      (rec.gambling.poker)
    • Re: is anyone here going to help me??? does anyone here know anything at all???
      ... I am very sure that if I post my broken template and they ... Until like three or four days ago, everything on my blog displayed ... The Google ads for Ad Sense, etc., in the right sidebar displayed perfectly. ... I tried to publish and got a broken HTML error. ...
      (rec.gambling.poker)