CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
template display

 



Gayu4justin
New User

Mar 26, 2013, 3:18 AM

Post #1 of 3 (650 views)
template display Can't Post

hai all,

This is my first post.
i want to display a template on text area while selecting a particular option from the selection box.

How can i achieve this using Perl???

Please reply....

Thanks to all in advance,
Gayu4justin


recruiter
User

Mar 26, 2013, 12:40 PM

Post #2 of 3 (644 views)
Re: [Gayu4justin] template display [In reply to] Can't Post

You can start by using a module, many of them are out there that do this. HTML::Template, Template::Toolkit, dancer, mojo.


Zhris
Enthusiast

Mar 27, 2013, 3:38 PM

Post #3 of 3 (636 views)
Re: [Gayu4justin] template display [In reply to] Can't Post

Hey,

If I understand you correctly, you want the value of a textarea to be based on the selected option of a select menu.

Here is a very rough example, making vigorous use of the CGI module:


Code
#!/usr/bin/perl 
use strict;
use warnings FATAL => qw/all/;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;

#####

my $templates = { A => 'This is template A',
B => 'This is template B',
C => 'This is template C' };

my $default_template = 'A';

my $cgi = CGI->new( );

my $template_content = $templates->{ $cgi->param('template') // $default_template } // $templates->{ $default_template };

#####

print $cgi->header( );

print $cgi->start_html( );

print $cgi->start_form( );

print $cgi->popup_menu( -name => 'template',
-values => [ sort keys %$templates ],
-default => $default_template,
-onChange => "window.location='?template='+this.value" );

print q{<noscript>} . $cgi->submit( -value => 'Update' ) . q{</noscript>};

print q{<br />} . $cgi->textarea( -name => 'template_content',
-default => $template_content,
-override => 1, # required otherwise default when script first invoked is used (sticky).
-rows => 10,
-columns => 100 );

print $cgi->end_form( );

print $cgi->end_html( );

#####

exit( );



- I have stored "templates" in a hashref which can be accessed by the value of the selected option.
- CGI's param() method can be used get the value of the selected option. I ensure if no value, i.e. first time script is invoked, or unknown value, then a default value is used.
- It would be nicer to use a template module, i.e. Template::Toolkit, rather than using CGI's methods to create HTML elements. However, my opinion is its easier for beginners to learn the CGI way first.
- I have used a javascript onChange event to reinvoke the script on option select, with the appropriate query string. If javascript is off, I have used <noscript> tags to display a submit button instead.

Apologies if my explanation goes over your head. Hope this helps,

Chris


(This post was edited by Zhris on Mar 27, 2013, 3:48 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives