Sunday, December 18, 2011

The Selection and Design of Templating in LedgerSMB 1.3

One of the key features of LedgerSMB 1.3 has been the move from the custom templating solution of LedgerSMB 1.2 (inherited from SQL-Ledger) to Template Toolkit.  This gives us more flexibility, better cross-language support since TT has been implemented for other languages as well, and the like.  However in practice in order to preserve these and avoid too close of a merging between business and presentation logic, we avoid some functions of TT, such as embedded Perl.  We also make have some specific approaches to how we invoke TT in order to avoid problems and help ensure standards compliance to the best of our ability.

The big problem that one runs into is the use of percents, which denote comments in LaTeX.  Leaving the percent signs in leads to parsing problems when testing templates.  So it became clear that since we wanted to re-use the same template logic for both UI and printed invoices the percent signs would have to go.

We settled on opening the tag with <?lsmb and closing it with ?>  The reason here is that this re-uses the SGML-standard "program instruction" tag syntax to ensure that validation can be run on the templates themselves.

A second thing we did was to add some template-specific functions.  These handle internationalization and number formatting.  Each of these has two forms:  a general form specific to the language or preferences of the template and a non-specific form, where the localization settings must be specified in the template.  This provides a consistent templating API for presentation of data, without mixing object behavior with the template layer itself.

The reason for these two forms is that there may be cases where number formatting must be specified in the template (such as when a check is printed), but there are others where user-specific settings should be used.  Similarly we want to specify a primary language with a template, but might want to translate strings into a second language (for example for bilingual receipts).

This gives us a general presentation API largely based on TT, but with differences which enable us to do whatever we need to do.  We had to rip out the old pagebreak handling and go instead to LaTeX longtables instead, which is probably a win anyway.

However one surprising use has come up, and that has been the ability to re-use the templating system in preparing database upgrade scripts which are then passed along to psql.

No comments:

Post a Comment