Separation of data from code is a good first step
Previously I have attempted to separate an HTML template from the functional code, admittedly not using a pre-built templating system. This was for a system that provides a business overview of KPIs to my senior management team both on demand as a webpage and as an email twice weekly. This code that was produced quite a few years ago is still operational and providing good service.
Here is an extract from the template file:
<div style="float:right">
<p class="sectionHead"><b>FHL Properties:</b></p>
<table>
<tr>
<th class="itemHead">Bookings this month:</th>
<td>%BOOK_MTH% </td>
</tr><tr>
<th class="itemHead">Previous 30 days:</th>
<td>%BOOK_PREV% </td>
</tr><tr>
<th class="itemHead">Forthcoming 30 days:</th>
<td>%BOOK_30% </td>
</tr><tr>
<th class="itemHead">Forthcoming 60 days:</th>
<td>%BOOK_60% </td>
</tr>
</table>
</div>
<hr style="clear:both">
<p class="sectionHead"><b>Progress in Property Event:</b></p>
<p class="itemHeadLarge">%EVENT_COUNT% people have registered their in
+terest</p>
<hr>
<p class="sectionHead"><b>Customer Contentment Call List:</b></p>
<p class="itemHeadLarge">There are %CALLLIST% people are on the Call L
+ist</p>
<hr>
with the %MARKUP% being substituted for the calculated values by the delivery code
$pulse =~ s/%EMAIL_VIEW_PERCENT%/$empview/g;
$pulse =~ s/%EMAIL_CLICK_PERCENT%/$empclick/g;
$pulse =~ s/%EMAIL_UNSUB_PERCENT%/$empunsub/g;
$pulse =~ s/%SHORTLIST%/$shortlist/g;
$pulse =~ s/%CALLLIST%/$calllist/g;
In practice this is difficult to maintain as there are some 50 items of data to substitute as some KPIs have multiple values and some require pluralisation whilst other don't. From this limited experience quite some time ago, templating seems more trouble than value...or is this not what you mean?
If there is a way to simplify it which can be quickly implemented then I would certainly change my approach.
One problem I have faced in the past with this code is that the path to the template file is hard-coded to the production version so I have to remember to change this in the test environment before making changes and swap it back before releasing it back to the wild. It was done this way because the script runs from cron to provide the twice weekly emails. It will be simpler in future because fellow Monks have introduced me to use FindBin; and use lib; that will make this particular application a great deal simpler...perhaps that change alone will revise my experience of HTML templates. |