OpenInteract2/ContentGenerator/HtmlTemplate.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 85 CC 5 SCAR 24.8
CC
5
Cov
0%
generate
30.0
initialize
2.0
BEGIN
2.0
CRAP 12.0
1 package OpenInteract2::ContentGenerator::HtmlTemplate;
2  
3 # $Id: HtmlTemplate.pm,v 1.8 2005/03/17 14:58:00 sjn Exp $
4  
5 use strict;
6 use base qw( OpenInteract2::ContentGenerator );
7  
8 use HTML::Template;
9 use Log::Log4perl qw( get_logger );
10 use OpenInteract2::Constants qw( :log );
11 use OpenInteract2::ContentGenerator::TemplateSource;
12 use OpenInteract2::Context qw( CTX );
13 use OpenInteract2::Exception qw( oi_error );
14  
15 $OpenInteract2::ContentGenerator::HtmlTemplate::VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
16  
17 my ( $log );
18  
19 # HTML::Template doesn't seem to use the same architecture as TT, so
20 # it doesn't make sense to create a template object in initialize()
21 # and reuse it.
22  
23 sub initialize {
24 my $log_init = get_logger( LOG_INIT );
25 $log_init->is_info &&
26 $log_init->info( "Called initialize() for HTML::Template CG (no-op)" );
27 }
28  
29 sub generate {
30 my ( $self, $template_config, $template_vars, $template_source ) = @_;
31 $log ||= get_logger( LOG_TEMPLATE );
32  
33 # TODO: Check for cached content...
34  
35 my %init_params = ( die_on_bad_params => 0 );
36  
37 my ( $source_type, $source ) =
38 OpenInteract2::ContentGenerator::TemplateSource->identify( $template_source );
39 if ( $source_type eq 'NAME' ) {
40 my ( $template, $filename, $modified ) =
41 OpenInteract2::ContentGenerator::TemplateSource->load_source( $source );
42 $log->is_debug &&
43 $log->debug( "Loading from name $source" );
44 $init_params{scalarref} = ( ref $template eq 'SCALAR' )
45 ? $template : \$template;
46 $init_params{option} = 'value';
47 }
48 elsif ( $source_type eq 'FILE' ) {
49 $init_params{filename} = $source;
50 $init_params{option} = 'value';
51 }
52 else {
53 $log->error( "Don't know how to load from source $source_type" );
54 return "Cannot process template from source $source_type";
55 }
56  
57 my $template = HTML::Template->new( %init_params );
58 $template->param( $template_vars );
59 my $content = $template->output;
60 unless ( $content ) {
61 my $msg = "Failed to fill in template for some unknown reason...";
62 $log->error( $msg );
63 oi_error $msg ;
64 }
65  
66 # TODO: Cache content before returning
67  
68 return $content;
69 }
70  
71 1;
72  
73 __END__