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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.