Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dear Monks,

Below is a simple cgi script I wrote to display different messages based on the value for $item using HTML Template

Everything worked fine and I didn't think about raising this question here until I read the following from HTML::Template Documentation

WARNING: Much of the benefit of HTML::Template is in decoupling your Perl and HTML. If you introduce numerous cases where you have TMPL_IFs and matching Perl if()s, you will create a maintenance problem in keeping the two synchronized. I suggest you adopt the practice of only using TMPL_IF if you can do so without requiring a matching if() in your Perl code.

Below is the script and template:-

use CGI qw/:standard :delete_all :escapeHTML :html3 :all/; use HTML::Template; print header; my $item = "CONTINUE"; my ($item1_val, $item2_val, $item3_val ) = 0; if ($item eq "SELECT") { $item1_val = 1; } elsif ($item eq "CONTINUE") { $item2_val = 1; } elsif ($item eq "DENIED"){ $item3_val = 1; } my $template = HTML::Template->new( filename => 'template4.tmpl' ); $template->param( item1 => $item1_val ); $template->param( item2 => $item2_val ); $template->param( item3 => $item3_val ); print $template->output();

Template:-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>HTML::Template example</title> </head> <body> <TMPL_IF name = "item1"> <table align="center"> <tr><td align="center"> <font style="font-size: 14pt; font-family: Arial; solid #CCC; padding: + 3px" color="blue"> <p>Please Select an Item</p></font> </td></tr> </table> </TMPL_IF> <TMPL_IF name = "item2"> <table align="center"> <tr><td align="center"> <font style="font-size: 14pt; font-family: Arial; solid #CCC; padding: + 3px" color="blue"> <p>Item Selected: Car</p></font> </td></tr> <tr><td align="center"> <font style="font-size: 8pt; font-family: Arial; solid #CCC; padding: +3px" color="black"> <p>(Please click to continue)</p></font> </td></tr> <tr><td align="center"> <input type="button" value="submit"/> </td></tr> </table> </TMPL_IF> <TMPL_IF name = "item3"> <table align="center"> <tr><td align="center"> <font style="font-size: 14pt; font-family: Arial; solid #CCC; padding: + 3px" color="blue"> <p>Item Selected: Mini Van</p></font> </td></tr> <tr><td align="center"> <font style="font-size: 12pt; font-family: Arial; solid #CCC; padding: + 3px" color="red"> <b>Access to this Item is denied</b></font> </td></tr> </table> </TMPL_IF> </body> </html>

As you can see, I end up in a situation, where I have matching 'if' and 'TMPL_IF' loops in the perlscript and template, respectively

The other way I could think is to have a variable (say $html_output) in the script and use the cgi module to append the appropriate html code to the variable and finally display that using HTML::Template as follows:-

use CGI qw/:standard :delete_all :escapeHTML :html3 :all/; use HTML::Template; print header; my $item = "CONTINUE"; if ($item eq "SELECT") { $html_output = ...... } elsif ($item eq "CONTINUE") { $html_output = ...... } elsif ($item eq "DENIED"){ $html_output = ...... } my $template = HTML::Template->new( filename => 'template4.tmpl' ); $template->param( html_output => $html_output ); print $template->output();

corresponding template...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>HTML::Template example</title> </head> <body> <TMPL_VAR name = "html_output"> </body> </html>

In the above approach,I would be mixing scripts and html tags, which I hate to do.

Would you please suggest some ideas to handle this?


In reply to HTML::Template : How to separate code and html with lesser maintenance issues by sara2005

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-16 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found