<?xml version="1.0" encoding="windows-1252"?>
<node id="266830" title="Nirvana through the templating yin yang (TT2 / CGI.pm)" created="2003-06-18 10:19:21" updated="2005-08-10 14:44:18">
<type id="120">
perlmeditation</type>
<author id="114691">
Aristotle</author>
<data>
<field name="doctext">
&lt;p&gt;This is a technique I discovered a while ago. It will probably underwhelm a lot of people who may already have been using it for a while. However I've yet to see it explicitly discussed.&lt;/p&gt;

&lt;p&gt;I refer to TT2 and CGI.pm's HTML generation methods as yin and yang here because their common uses represent polar opposites in the emphasis on application- vs. presentation centric design. By combinging the two, you can follow presentation centric design and get stickiness for form elements very painlessly.&lt;/p&gt;

&lt;p&gt;So much fuss, so very little to show for it:&lt;/p&gt;

&lt;code&gt;
#!/usr/bin/perl -w
use strict;
use CGI;
use Template

# ...

my $q = CGI-&gt;new;

# ...

$template-&gt;process("foo.tmpl.html", {
    query      =&gt; $q,
    @other_variables,
}) or die $Template::ERROR;
&lt;/code&gt;

Now you can achieve sticky form elements simply by using TT2's object access capabilities to say things like

&lt;code&gt;
[% query.textfield("name", "J. Random Hacker", 30) %]
&lt;/code&gt;

&lt;p&gt;It is nothing earth shattering. But IMO, its usefulness is in fact because of rather than despite that.&lt;/p&gt;

&lt;p&gt;Another use might be to implement purely presentation relevant aspects of a web application completely outside the core CGI script - after all, nothing stops you from querying parameter values as in &lt;tt&gt;lang = query.param('lang');&lt;/tt&gt; in the template. I have used this to retrofit internationalization on a CGI script without touching the Perl code at all.&lt;/p&gt;

&lt;div class="pmsig pmsig-114691"&gt;
&lt;p align="right"&gt;&lt;em&gt;Makeshifts last the longest.&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;</field>
</data>
</node>
