Re: CGI.pm and HTML tables
From: Bill Karwin (bill_at_karwin.com)
Date: 12/18/04
- Previous message: ladygrinningsoul: "Re: CGI.pm and HTML tables"
- In reply to: ladygrinningsoul: "Re: CGI.pm and HTML tables"
- Next in thread: John Bokma: "Re: CGI.pm and HTML tables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Previous message: ladygrinningsoul: "Re: CGI.pm and HTML tables"
- In reply to: ladygrinningsoul: "Re: CGI.pm and HTML tables"
- Next in thread: John Bokma: "Re: CGI.pm and HTML tables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|