File Coverage

lib/Rex/Template/TT.pm
Criterion Covered Total %
statement 38 49 77.5
branch 11 18 61.1
condition 4 6 66.6
subroutine 9 9 100.0
pod 0 2 0.0
total 62 84 73.8


line stmt bran cond sub pod time code
1             #
2             # Nicolas Leclercq
3             #
4             # vim: set ts=2 sw=2 tw=0:
5             # vim: set expandtab:
6              
7             package Rex::Template::TT;
8             $Rex::Template::TT::VERSION = '0.33.1.1'; # TRIAL
9 2     2   32857 use Rex -base;
  2         5  
  2         13  
10 2     2   339260 use Rex::Helper::Path;
  2         13  
  2         131  
11 2     2   28 use Template;
  2         40938  
  2         38  
12              
13             require Exporter;
14 2     2   88 use base qw(Exporter);
  2         8  
  2         155  
15 2     2   11 use vars qw (@EXPORT);
  2         3  
  2         1185  
16              
17             @EXPORT = qw(template_toolkit);
18              
19              
20             sub validate_vars {
21 8     8 0 46 my ( $vars ) = @_;
22              
23 8         98 foreach my $key (keys %$vars) {
24 184 50       607 if ($key =~ /^[\._]/) {
    50          
    50          
25 0         0 Rex::Logger::info( "variable name '$key' considered private by Template Toolkit",
26             "warn" );
27             } elsif ($key =~ /([^a-zA-Z0-9_])/) {
28 0         0 Rex::Logger::info( "variable name '$key' contains '$1' unsupported by Template Toolkit",
29             "warn" );
30             } elsif ($key =~ /^(GET|CALL|SET|DEFAULT|INSERT|INCLUDE|PROCESS|WRAPPER|IF|UNLESS|ELSE|ELSIF|FOR|FOREACH|WHILE|SWITCH|CASE|USE|PLUGIN|FILTER|MACRO|PERL|RAWPERL|BLOCK|META|TRY|THROW|CATCH|FINAL|NEXT|LAST|BREAK|RETURN|STOP|CLEAR|TO|STEP|AND|OR|NOT|MOD|DIV|END)$/) {
31 0         0 Rex::Logger::info( "variable name '$key' clashes with reserved Template Toolkit word",
32             "warn" );
33             }
34             }
35             }
36              
37             sub template_toolkit {
38 4     4 0 1123000 my ( $template_path, $vars ) = @_;
39              
40 4         57 validate_vars( $vars );
41              
42 4 100 66     73 if ( not ( ref $template_path and ref $template_path eq 'SCALAR') ) {
43             # resolv template path
44 2         34 $template_path = Rex::Helper::Path::resolv_path($template_path);
45 2         53 $template_path = Rex::Helper::Path::get_file_path( $template_path, caller() );
46 2         608 Rex::Logger::debug("Processing template file : $template_path");
47             }
48              
49             # process template
50 4         49 my $output = '';
51             my $template = Template->new( { ABSOLUTE => 1 } )
52 4 50       175 or do {
53 0         0 Rex::Logger::info( $Template::ERROR, 'error' );
54 0         0 die $Template::ERROR;
55             };
56             $template->process( $template_path, $vars, \$output )
57 4 50       28454 or do {
58 0         0 Rex::Logger::info( $template->error(), 'error' );
59 0         0 die $template->error();
60             };
61              
62 4         31454 return $output;
63             }
64              
65             sub import {
66              
67 2     2   27 my ( $class, $tag ) = @_;
68              
69 2 100 66     24 if ( $tag && $tag eq ":register" ) {
70              
71             # register Template::Toolkit for default template processing
72             set template_function => sub {
73 4     4   1538893 my ( $content, $vars ) = @_;
74              
75 4         170 validate_vars( $vars );
76              
77             my $template = Template->new()
78 4 50       153 or do {
79 0         0 Rex::Logger::info( $Template::ERROR, 'error' );
80 0         0 die $Template::ERROR;
81             };
82 4         26166 my $output;
83             $template->process( \$content, $vars, \$output )
84 4 50       33 or do {
85 0         0 Rex::Logger::info( $template->error(), 'error' );
86 0         0 die $template->error();
87             };
88              
89 4         34786 return $output;
90 1         17 };
91             }
92              
93 2         601 __PACKAGE__->export_to_level(1);
94             }
95              
96             1;
97              
98             =pod
99              
100             =head1 NAME
101              
102             Rex::Template::TT - A module to process templates with template toolkit.
103              
104             =head1 VERSION
105              
106             version 0.33.1.1
107              
108             =head1 SYNOPSIS
109              
110             use Rex::Template::TT;
111              
112             task "blah", sub {
113             file "/tmp/blah",
114             content => template("path/to/blah.template",
115             { persons => ['bob', 'alice'] }),
116             owner => "root",
117             group => "root",
118             mode => 644
119             };
120              
121             # to use as a default template engine
122             # this will make the template() function use TemplateTookit to render
123             # all the templates. This will also register all the known template variables
124             # like hostname, eth0_ip and so on.
125             use Rex::Ext::TemplateTookkit ':register';
126              
127              
128             =head1 DESCRIPTION
129              
130             A Rex extension module to process templates with template toolkit.
131              
132             =head1 AUTHORS
133              
134             This module in its current publication is written by Nicolas Leclerq, based
135             on prior work by Jan Gehring. The original version by Nicolas was published
136             through the now-defunct I. Since Nicolas's version ended up
137             being more feature-rich than Jan's, Jan gave permission to overwrite his
138             version with Nicolas's.
139              
140             =over
141              
142             =item * Nicolas Leclercq
143              
144             =item * Jan Gehring
145              
146             =back
147              
148             =head1 CONTRIBUTORS
149              
150             =over
151              
152             =item * Erik Huelsmann
153              
154             =back
155              
156             =head1 SEE ALSO
157              
158             =over
159              
160             =item * L