| 1 | package RWDE::Web::TemplateAdapter; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Error qw(:try); | ||
| 7 | use Template; | ||
| 8 | |||
| 9 | use RWDE::Configuration; | ||
| 10 | use RWDE::RObject; | ||
| 11 | |||
| 12 | use base qw(RWDE::Singleton); | ||
| 13 | |||
| 14 | use vars qw($VERSION); | ||
| 15 | $VERSION = sprintf "%d", q$Revision: 509 $ =~ /(\d+)/; | ||
| 16 | |||
| 17 | my $unique_instance; | ||
| 18 | |||
| 19 | sub get_instance { | ||
| 20 | my ($self, $params) = @_; | ||
| 21 | |||
| 22 | if (ref $unique_instance ne $self) { | ||
| 23 | $unique_instance = $self->new; | ||
| 24 | } | ||
| 25 | |||
| 26 | return $unique_instance; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub initialize { | ||
| 30 | my ($self, $params) = @_; | ||
| 31 | |||
| 32 | my @types = ('rwp', 'ttml', 'bin', 'rss'); | ||
| 33 | |||
| 34 | foreach my $type (@types) { | ||
| 35 | |||
| 36 | $self->{$type} = Template->new( | ||
| 37 | { | ||
| 38 | TAG_STYLE => 'asp', | ||
| 39 | PROCESS => 'types/' . $type . '.tt', | ||
| 40 | POST_CHOMP => 1, | ||
| 41 | PRE_CHOMP => 1, | ||
| 42 | INCLUDE_PATH => RWDE::Configuration->get_root() . '/templates/components:' . RWDE::Configuration->get_root() . '/templates', | ||
| 43 | PLUGIN_BASE => 'Plugins', | ||
| 44 | VARIABLES => { commify => \&RWDE::Utility::commify, }, | ||
| 45 | COMPILE_DIR => '/tmp/templates', | ||
| 46 | COMPILE_EXT => '.tc', | ||
| 47 | } | ||
| 48 | ) or die "Template::new failure"; | ||
| 49 | } | ||
| 50 | |||
| 51 | return (); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub render { | ||
| 55 | my ($self, $params) = @_; | ||
| 56 | |||
| 57 | my @required = qw( helper ); | ||
| 58 | RWDE::RObject->check_params({ required => \@required, supplied => $params }); | ||
| 59 | |||
| 60 | my $template_adapter = $self->get_instance(); | ||
| 61 | |||
| 62 | my $type = $$params{helper}->get_pagetype(); | ||
| 63 | |||
| 64 | unless (defined $type and defined $template_adapter->{$type}) { | ||
| 65 | throw RWDE::DevelException({ info => "There is an error with requested type: $type (possibly badly separated, or not defined for uri: " . $$params{helper}->get_uri() }); | ||
| 66 | } | ||
| 67 | |||
| 68 | my $t = $template_adapter->{$type}; | ||
| 69 | |||
| 70 | my $template_name = $$params{helper}->get_uri(); | ||
| 71 | my $vars_ref = $$params{helper}->get_stash(); | ||
| 72 | |||
| 73 | unless ($t->process($template_name, $vars_ref)) { | ||
| 74 | throw RWDE::DevelException({ info => 'There is an error: ' . $t->error() . "in template: $template_name" }); | ||
| 75 | } | ||
| 76 | |||
| 77 | return (); | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; |