Changes between Initial Version and Version 1 of Development/Code/TemplateToolkit


Ignore:
Timestamp:
Apr 17, 2012, 8:53:42 PM (13 years ago)
Author:
munoz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Development/Code/TemplateToolkit

    v1 v1  
     1= Using Template::Toolkit in Quattor components =
     2
     3In 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.
     4
     5For instance, the accounts component could do.
     6
     7{{{
     8#!perl
     9
     10# WARNING: UNTESTED!!!
     11
     12use Template;
     13use CAF::FileWriter;
     14
     15my $fh = CAF::FileWriter->new("/etc/passwd", log => $self);
     16my $tree = $config->getElementGetTree();
     17# This one is needed for Template::Toolkit to work with the CAF::File*
     18my $tpl = Template->new();
     19$tpl->process(\*DATA, $tree, $fh);
     20
     21__DATA__
     22
     23[% FOREACH user in users.pairs %]
     24[% user.key %]:x:[% user,value.uid %]:[% user.value.gid %]:[% user.value.comment %]:[% user.value.homeDir %]:[% user.value.shell %]
     25[% END %]
     26}}}
     27
     28The advantage in some sitiations is that the representation is completely isolated from the logic that will generate it. And it may allow for a closer correspondency with the schema, if wanted.
     29
     30Sometimes one template won't be enough: there will be several files of several types to print (f.i, nagios-related configuration). We still have to see how to ship different templates.
     31
     32Also, we have to be careful so that we don't replace ugly code with even uglier templates!!