Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

td() and Tr() inCGI.pm

by thunders (Priest)
on Nov 26, 2001 at 00:36 UTC ( #127418=perlquestion: print w/replies, xml ) Need Help??

thunders has asked for the wisdom of the Perl Monks concerning the following question:

I have a question regarding the CGI module. I'd rather use the CGI functions to put all of my HTML together, But In this one case I can't figure out how to do it the way I want to. Take the following sample.
print table( Tr( [ td(['First Name:', textfield(-name=>"first_name", -maxle +ngth=>25, -size=>12), 'Last Name:' , textfield(-name=>"last_name", -maxl +ength=>25, -size=>12) ]), td({ -colspan=>2 },['Address:' , textarea(-name=>"addres +s", -rows=>10, -columns=>50)]), ] ) );

This creates a table with two rows. The fist row has four columns for two textboxes and two labels. I want the second row to have one column with a colspan of one and one column with a colspan of three. The docs don't specify a way to distribute atrributes among a group of td elements. I know how to give each td in the row a colspan of 2 as above, but how do I give two different colspans to the td elements? Is it possible when using the Tr([]) shortcut?

Replies are listed 'Best First'.
Re: td() and Tr() inCGI.pm
by chipmunk (Parson) on Nov 26, 2001 at 02:07 UTC
    For the second row, use two separate calls to td(), instead of one combined call, and specify the appropriate attributes for each call.
    print table( Tr( [ td(['First Name:', textfield(-name=>"first_name", -maxle +ngth=>25, -size=>12), 'Last Name:' , textfield(-name=>"last_name", -maxl +ength=>25, -size=>12) ]), join(' ', td('Address'), td({ -colspan=>2}, textarea(-na +me=>"address", -rows=>10, -columns=>50))), ] ) );
      Thanks chipmunk that was what I was looking for, and using join() was clever. I had thought of using two td calls but I couldn't figure out how to do that in the context of Tr() without creating a new row.
        I agree it's an interesting trick, and I upvoted it because he answered your question in the way you asked him to, but from a general software engineering perspective it's a lousy practice. In this case there is no good reason to cram all those table cells into the smallest possible number of HTML shortcuts. The resulting code is hard to write, harder to read, and painful to maintain. This adds up to projects which take too long and cost too much if you code like this as a way of life. cLive;-)'s answer below is the Right Way to do this with HTML shortcuts.
(jeffa) Re: td() and Tr() inCGI.pm
by jeffa (Bishop) on Nov 26, 2001 at 00:57 UTC
    You really can't do that directly, take a look at Re: THe td and Tr functions of CGI.pm. Sorry, i had some code posted here, but it didn't do the job. To be quite honest, trying to generate fine grained HTML like that is better suited for another module. Have you seen HTML::Template yet? (hint, hint) ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: td() and Tr() inCGI.pm
by cLive ;-) (Prior) on Nov 26, 2001 at 07:48 UTC
    For things like this, I like to make it easier to read (even if it makes it a little longer), by expanding the code into a more readable form:
    print table( Tr( td('First Name:'), td(textfield(-name=>"first_name", -maxlength=>25, -size=>12, ), ), td('Last Name:'), td(textfield(-name=>"last_name", -maxlength=>25, -size=>12, ), ), ), Tr( td({-colspan=>2}, 'Address:', ), td({-colspan=>2}, textarea(-name=>"address", -rows=>10, -columns=>50, ), ), ), );

    Note, I also put commas where they aren't specifically needed - over time, I've found that if you do this, when you come to amend the table, you don't have to worry about where they should be - extras at ends of elements are ignored.

    .02

    cLive ;-)

    update - thanks for typo correction from jeffa

Re: td() and Tr() inCGI.pm
by kwoff (Friar) on Nov 26, 2001 at 02:08 UTC
    I agree with using HTML::Template if you can.
    That said, I've done something like that, and I just concatenated the td() together.
    my $graph = $q->img({-src=>"blank.gif", -alt=>"$percentage\%"}); my $graph_table = $q->table({-width=>'100%'}, $q->Tr([ $q->td({-height=>10, -width=>"${percent_label}\%"}, [$graph]) . $q->td({-height=>10, -width=>(100-$percentage).'%'}, [$graph]) ]));

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://127418]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-06-27 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (88 votes). Check out past polls.

    Notices?