http://qs321.pair.com?node_id=585194

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

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?