Changes between Version 2 and Version 3 of Development/Code/TemplateToolkit


Ignore:
Timestamp:
Apr 25, 2012, 1:46:25 AM (12 years ago)
Author:
munoz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Development/Code/TemplateToolkit

    v2 v3  
    11= Using Template::Toolkit in Quattor components =
     2
     3== Introduction ==
    24
    35In some cases, most of the code deals with printing correctly to a file, and it could be simplified a lot by using a basic template instead of zillions of print statements.
     
    3436
    3537Also, we have to be careful so that we don't replace ugly code with even uglier templates!!
     38
     39== Proposal for the Template Toolkit ==
     40
     41=== Template storage ===
     42
     43Since there may be several components using the Template toolkit, I suggest to store them under `/usr/share/quattor/templates`. We'd then create one subdir for each component, like this:
     44
     45 * `/usr/share/quattor/templates/accounts/`
     46 * `/usr/share/quattor/templates/sudo/`
     47
     48Does it cause any confusion with the Pan templates (mis-)installed under `/usr/share/doc`? If so, we can choose `/usr/share/quattor/tt`.
     49
     50=== Invoking the templates ===
     51
     52All components using `Template::Toolkit` will need the smae configuration, namely; the `INCLUDE_PATH` (that should be configurable) and the ability to access variables that start with an underscore. It's easiest to encapsulate all this in the base class. Then, our component would just request this base class the Template object:
     53
     54{{{
     55#!perl
     56my ($self, $config) = @_;
     57
     58my $t = $config->getElement("/software/components/foo")->getTree();
     59my $tpl = $self->template();
     60my $fh = CAF::FileWriter->open("/etc/file", log => $self);
     61print $fh "#A common header\n";
     62$tpl->process("component/template.tt", $t, $fh);
     63}}}
     64
     65Alternatively, the base class '''may''' do the parsing on our behalf ('''is this preferred?'''):
     66
     67{{{
     68#!perl
     69my $t = ...->getTree();
     70my $fh = CAF::FileWriter->open(...);
     71$self->process_template("component/template.tt", $t, $fh);
     72}}}