Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Calling functions in middle of CGI HTML declarations

by Don Coyote (Monk)
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;

Prints

Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" 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>

Coyote


Comment on Re: Calling functions in middle of CGI HTML declarations
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-10-02 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls