Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Calling functions in middle of CGI HTML declarations

by Don Coyote (Pilgrim)
on Aug 26, 2012 at 15:04 UTC ( #989833=note: print w/replies, xml ) Need Help??

in reply to Calling functions in middle of CGI HTML declarations

Another way would be to place the array as an argument to the div method directly. You can get different results from slightly different constructs. Please note I am using the object orientated syntax. I have updated this reply with the syntax for the method orientated syntax. See end of post.

Placing the array directly as an argument will result in the list being expanded as content.

print $q->div( { -id => "div-test" }, $q->div(@fields), ); <body> <div id="div-test"> <div>Hello Goodbye Friend Adios Amigo</div> </div> </body>

To reproduce a singular div for every item in the list then let the method know you are supplying a list by placing square brackets around the array.

print $q->div( { -id => "div-test" }, $q->div([@fields]), ); <body> <div id="div-test"> <div>Hello</div> <div>Goodbye</div> <div>Friend</div> <div>Adios</div> <div>Amigo</div> </div><!--end div-test div --> </body>

Using a foreach loop may become necessary if you perhaps wanted the id of each div to be entitled as per each item of the array. But while in the first two instances you would be placing the array into the content compartment of the argument to div, this would be a standalone statement for cgi to process. That is, placed outside of any arguments to div, if you wanted to you could write a quick div method to wrap any arrays of divs.

print $q->div( { -id => "div-test" }, $q->div([@fields]), ); print ( $q->start_div({-id=>$_}), $q->end_div ) foreach @fields; <body> <div id="div-test"> <div>Hello</div> <div>Goodbye</div> <div>Friend</div> <div>Adios</div> <div>Amigo</div> </div><!--end div-test div --> <div id="Hello"></div> <div id="Goodbye"></div> <div id="Friend"></div> <div id="Adios"></div> <div id="Amigo"></div> </body>

Have a play around with those and you should be able to get what you want.

Update UTC 19:55 27 08 2012

I have revisited my reply to adjust for using the method calling approach. Most of the previous works by simply removing the $q calls. However to enable the final foreach statement to work you will also need to import the div element methods as well as :standard.

use CGI qw/:standard *div/;

This allows you to use start_div and end_div methods. The glob in front of element allows you to call any start_element, end_element pair from your script. I have also included the reference syntax as shown by Your Mother in reply

#! /usr/bin/perl use strict; use warnings; use CGI qw /:standard *div/; use CGI::Pretty; my $q = CGI->new(); my @fields = qw(Hello World); print header(), start_html(), div( @fields ), div( [@fields] ), div( { -id => "div-test" }, div(\@fields) ); print ( start_div({-id=>$_}), end_div() ) foreach @fields; print end_html(); exit 0;


Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns="" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <div> Hello World </div> <div> Hello </div> <div> World </div> <div id="div-test"> <div> Hello </div> <div> World </div> </div> <div id="Hello"> </div> <div id="World"> </div> </body> </html>


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989833]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-05-27 12:33 GMT
Find Nodes?
    Voting Booth?