I knew when I star­ted this pro­ject (which was a while ago, I kinda cheated on this one) that I didn’t want to write out all the HTML myself, I’m not THAT mas­ochistic. What I wanted to do was basic­ally say “make a text box to write the address in”.

So I star­ted with a gen­eric pre­pro­cessor. I’ve use pre­pro­cessors such as CPP, TeX and Make so figured I would take a gen­eric pre­pro­cessor like M4 and define a few HTML mac­ros… prob­lem solved. Unfor­tu­nately it wasn’t that simple and as I added com­plex­ity it got harder to keep it all straight, par­tic­u­larly the quoting.

The basic text box wasn’t too bad.

dnl TEXT_Q(name, label, [length])
define(TEXT_Q, indir(LAYOUT, $1,
‘<label for=”$1_text”>$2</label>’,
‘<input id=”$1_text” name=”$1ifelse($3, ‘’, ‘’, ‘size=”$3″’)>’

But when I tried to cre­ate optional group­ings it star­ted to get messy. This is a snip­pit from TABULAR_OPTIONAL_ROWS and call­ing it.

dnl Build the head­ing rows
<tr class=“tabular_optional_rows_heading_row”>
foreach(‘XXXXX’, (dquote(shift($@))), ‘XXXXX’)

TEXT_Q(salary_abn, Pay­ers Aus­tralian busi­ness num­ber, 11), dnl TODO: Should be Payer’s

With TABULAR_OPTIONAL_ROWS I couldn’t fig­ure out how to have a vari­able num­ber of rows and I couldn’t fig­ure out how to pass it’s options without quot­ing so I decided to throw in the towel and switch to some­thing else.

I had exper­i­mented with Tem­plate Toolkit at work using it to inject snip­pits of Javas­cript into HTML doc­u­ments. In the end I stopped using it and had a static HTML page which included a fully gen­er­ated Javas­cript file but the short expos­ure I had impressed me and I tried it here. I found it much cleaner, the named para­met­ers cut out a lot of com­plex­ity and the abil­ity to just drop back to Perl if I wanted to do some­thing really hairy was nice.

This is the defin­i­tion of a text box fol­lowed by the call­ing syn­tax of a radio box that toggles the vis­ib­il­ity of the text box.

<tr class=“text” id=[% name %]_row”
[%- IF hide -%]
style=“dis­play: none“
[%- END -%]
<td class=“ques­tion”>
<label for=[% name %]_id”>[% text %]</label>
<td class=“answer”>
<input type=“text” name=[% name %] id=[% name %]_id”>

[% INCLUDE tax/radio
name = “surname_changed“
text = “Has any part of your name changed since com­plet­ing your last tax return?“
modify = “previous_surname“
options = [{name => “Yes”, action => “show”}, {name => “No”, action => “hide”}]

[% INCLUDE tax/text
name = “previous_surname“
text = “Pre­vi­ous sur­name“
hide = “true“

The nor­mal syn­tax for options is simply [“Yes”, “No”], the Perl fall­back I men­tioned earlier allows me to have more com­plex syn­tax when I need it but keeps things simple most of the time. I’ve since dis­covered there’s a Tem­plate Toolkit ver­sion three that’s a nice step for­ward but it’s lis­ted as still in devel­op­ment and in a very Alpha state, it also smelt a bit like it was suf­fer­ing from clas­sic second pro­ject woes. I think I’m play­ing with enough new stuff not to add exper­i­mental Tem­plate Toolkit ver­sions to the list.