| 1 | = Using Template::Toolkit in Quattor components = |
| 2 | |
| 3 | In 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 | |
| 5 | For instance, the accounts component could do. |
| 6 | |
| 7 | {{{ |
| 8 | #!perl |
| 9 | |
| 10 | # WARNING: UNTESTED!!! |
| 11 | |
| 12 | use Template; |
| 13 | use CAF::FileWriter; |
| 14 | |
| 15 | my $fh = CAF::FileWriter->new("/etc/passwd", log => $self); |
| 16 | my $tree = $config->getElementGetTree(); |
| 17 | # This one is needed for Template::Toolkit to work with the CAF::File* |
| 18 | my $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 | |
| 28 | The 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 | |
| 30 | Sometimes 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 | |
| 32 | Also, we have to be careful so that we don't replace ugly code with even uglier templates!! |