| 1 | package HPCI::Template; | ||
| 2 | |||
| 3 | use namespace::autoclean; | ||
| 4 | |||
| 5 | use Moose::Role; | ||
| 6 | |||
| 7 | use autodie; | ||
| 8 | use Carp; | ||
| 9 | use Params::Validate ':all'; | ||
| 10 | use Text::Xslate; | ||
| 11 | |||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | HPCI::Template - This role gives the consumer a simple templating engine | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Provides a method render_template_to_file that writes a target file, | ||
| 20 | using a Text::Xslate template file to define the content. | ||
| 21 | |||
| 22 | =head1 Attributes | ||
| 23 | |||
| 24 | =over 4 | ||
| 25 | |||
| 26 | =item * template_search_paths - array of directory strings to search for template files | ||
| 27 | |||
| 28 | =item * _template_renderer - (internal) A Text::Xslate instance. | ||
| 29 | |||
| 30 | =back | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | |||
| 35 | has 'template_search_paths' => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'ArrayRef[Str]', | ||
| 38 | lazy => 1, | ||
| 39 | default => sub { | ||
| 40 | my $self = shift; | ||
| 41 | my $tdir = $self->_template_dir; | ||
| 42 | my $clus = $self->cluster; | ||
| 43 | [ "$tdir/$clus", $tdir ] }, | ||
| 44 | ); | ||
| 45 | |||
| 46 | has '_template_dir' => ( | ||
| 47 | is => 'ro', | ||
| 48 | isa => 'Str', | ||
| 49 | init_arg => undef, | ||
| 50 | lazy => 1, | ||
| 51 | default => sub { | ||
| 52 | my $self = shift; | ||
| 53 | my $dir = File::ShareDir::module_dir('HPCI'); | ||
| 54 | print("Template search path : '$dir'\n"); | ||
| 55 | return $dir; | ||
| 56 | }, | ||
| 57 | ); | ||
| 58 | |||
| 59 | has '_template_renderer' => ( | ||
| 60 | is => 'ro', | ||
| 61 | isa => 'Text::Xslate', | ||
| 62 | init_arg => undef, | ||
| 63 | lazy => 1, | ||
| 64 | default => sub { | ||
| 65 | my $self = shift; | ||
| 66 | Text::Xslate->new( | ||
| 67 | path => $self->template_search_paths, | ||
| 68 | verbose => 2, | ||
| 69 | ); | ||
| 70 | }, | ||
| 71 | ); | ||
| 72 | |||
| 73 | =head1 Methods | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | =item * render_template_to_file - generate a file from a template | ||
| 78 | |||
| 79 | Usage: | ||
| 80 | $object->render_template_to_file( | ||
| 81 | template_name => "file.template", | ||
| 82 | output_file_path => "myfile.out", | ||
| 83 | rendering_variables => { | ||
| 84 | foo => "foo value", | ||
| 85 | bar => [ qw(bar has a bunch of values ...) ], | ||
| 86 | }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub render_template_to_file { | ||
| 94 | my $self = shift; | ||
| 95 | my %args = validate( | ||
| 96 | @_, | ||
| 97 | { | ||
| 98 | template_name => { | ||
| 99 | type => SCALAR, | ||
| 100 | }, | ||
| 101 | output_file_path => { | ||
| 102 | type => SCALAR, | ||
| 103 | }, | ||
| 104 | rendering_variables => { | ||
| 105 | type => HASHREF, | ||
| 106 | default => {} | ||
| 107 | } | ||
| 108 | } | ||
| 109 | ); | ||
| 110 | |||
| 111 | if (defined $self->meta->get_attribute('log')){ | ||
| 112 | $self->log->info("Rendering '".$args{template_name}."' to '".$args{output_file_path}."'") | ||
| 113 | } | ||
| 114 | |||
| 115 | my $variables_hash = $args{rendering_variables}; | ||
| 116 | |||
| 117 | # Render the template with the variables | ||
| 118 | my $rendered_contents = $self->_template_renderer->render($args{template_name}, $args{rendering_variables}); | ||
| 119 | |||
| 120 | # Write the result to the file | ||
| 121 | my $output_file_path = $args{output_file_path}; | ||
| 122 | open(my $output_file, '>', "$output_file_path"); | ||
| 123 | $output_file->print($rendered_contents); | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; |