2009/11/4 "Hendrik T. Völker" <[email protected]>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi guys,
>
> I have a problem regarding data defined inside the META tag.
>
> For example I have the following at the start of a template file:
>
> - ---8<---
> [%= META
> dest = '/etc/motd'
> user = 'root'
> group = 'root'
> mode = 444
> revision = '$Id: motd,v 1.4 2009/10/19 11:19:01 hvolker Exp $'
> descr = 'MOTD file of the system'
> =%]
> - ---8<---
>
> I'd like to access this defined data inside of the Perl script after I have
> processed the template with the following
>
> - ---8<---
> $template = Template->new({INCLUDE_PATH =>
> '/var/home/hvolker/config-gen-3'});
> $template->process (
> $proxy_cfg->{'template'}{$config}{'template_file'},
> $data,
> \$result
> );
> - ---8<---
>
> The result of this process is later stored inside a tar file using
> Archive::Tar
> and I'd like to use the data from the META section to set filename, user,
> grp,
> and mode inside of the tar archive.
>
> Anybody any idea how to do that properly?
>
>
Metadata lives in Template::Document objects, and all such objects call
&Template::Context::visit, so one approach is to subclass Template::Context
and override the visit method:
package My::Context;
use base qw(Template::Context);
sub new {
my ($class, $meta) = @_;
my $self = $class->SUPER::new;
$self->{ MYMETA } = $meta;
return $self;
}
sub visit {
my ($self, $document, $blocks) = @_;
my $hash = $self->{ MYMETA }{ $document->{name} };
if (!%$hash) {
$hash->{dest} = $document->{dest};
$hash->{user} = $document->{user};
# ...
}
return $self->SUPER::visit($document, $blocks);
}
And then:
my %meta;
my $context = My::Context->new({ 'my-template-file' => \%meta });
my $template = Template->new({ CONTEXT => $context });
$template->process(...);
print "revision metadata is $meta{revision}\n";
You might need more detailed logic to determine what template file a
particular Template::Document object came from (dealing with
absolute/relative paths, etc), but the basic idea seems sound.
--Sean
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates