| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | OpenFrame::WebApp::Template - abstract class for template processing wrappers | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | # abstract class - does nothing on its own | ||||||||||
| 8 | use OpenFrame::WebApp::Template::SomeClass; | ||||||||||
| 9 | |||||||||||
| 10 | my $tmpl = new OpenFrame::WebApp::Template::SomeClass() | ||||||||||
| 11 | ->file( $local_path ) | ||||||||||
| 12 | ->template_vars( { some => vars } ); | ||||||||||
| 13 | |||||||||||
| 14 | try { | ||||||||||
| 15 | $ofResponse = $tmpl->process; | ||||||||||
| 16 | } catch OpenFrame::WebApp::Template::Error with { | ||||||||||
| 17 | my $e = shift; | ||||||||||
| 18 | print $e->flag, $e->message; | ||||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | package OpenFrame::WebApp::Template; | ||||||||||
| 24 | |||||||||||
| 25 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 7 | ▶ | use warnings::register; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 7 | ▶ | use Error qw( :try ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 7 | ▶ | use OpenFrame::Response; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 7 | ▶ | use OpenFrame::WebApp::Error::Abstract; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 7 | ▶ | use OpenFrame::WebApp::Template::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 7 | ▶ | use base qw ( OpenFrame::Object ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 7 | our $VERSION = (split(/ /, '$Revision: 1.13 $'))[1]; | |||||||||
| 36 | |||||||||||
| 37 | 7 | our $TYPES = { | |||||||||
| 38 | tt2 => 'OpenFrame::WebApp::Template::TT2', | ||||||||||
| 39 | petal => 'OpenFrame::WebApp::Template::Petal', | ||||||||||
| 40 | }; | ||||||||||
| 41 | |||||||||||
| 42 | ## get hash of known template types | ||||||||||
| 43 | 17 | ▶ | sub types { | ||||||||
|
Subroutine
types: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||
| 44 | 17 | my $self = shift; | |||||||||
| 45 | 17 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | $TYPES = shift; | |||||||||
| 47 | 0 | return $self; | |||||||||
| 48 | } else { | ||||||||||
| 49 | 17 | return $TYPES; | |||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | ## object init | ||||||||||
| 54 | 10 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 55 | 10 | my $self = shift; | |||||||||
| 56 | 10 | $self->template_vars( {} ); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | ## template processor | ||||||||||
| 60 | 10 | ▶ | sub processor { | ||||||||
|
Subroutine
processor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 61 | 10 | my $self = shift; | |||||||||
| 62 | 10 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 3 | $self->{template_processor} = shift; | |||||||||
| 64 | 3 | return $self; | |||||||||
| 65 | } else { | ||||||||||
| 66 | 7 | return $self->{template_processor}; | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | ## override this method | ||||||||||
| 71 | 0 | ▶ | sub default_processor { | ||||||||
|
Subroutine
default_processor: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 72 | 0 | my $self = shift; | |||||||||
| 73 | 0 | throw OpenFrame::WebApp::Error::Abstract( class => ref($self) ); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | ## local path to the template file | ||||||||||
| 77 | 17 | ▶ | sub file { | ||||||||
|
Subroutine
file: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 78 | 17 | my $self = shift; | |||||||||
| 79 | 17 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 6 | $self->{template_file} = shift; | |||||||||
| 81 | 6 | return $self; | |||||||||
| 82 | } else { | ||||||||||
| 83 | 11 | return $self->{template_file}; | |||||||||
| 84 | } | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | ## hash of template processing vars | ||||||||||
| 88 | 16 | ▶ | sub template_vars { | ||||||||
|
Subroutine
template_vars: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 89 | 16 | my $self = shift; | |||||||||
| 90 | 16 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 12 | $self->{template_vars} = shift; | |||||||||
| 92 | 12 | return $self; | |||||||||
| 93 | } else { | ||||||||||
| 94 | 4 | return $self->{template_vars}; | |||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | ## process the template file | ||||||||||
| 99 | 4 | ▶ | sub process { | ||||||||
|
Subroutine
process: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 100 | 4 | my $self = shift; | |||||||||
| 101 | 4 | my $file = $self->file; | |||||||||
| 102 | |||||||||||
| 103 | 4 | ▶ | return ($self->file_not_found) unless (-e $file); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | 3 | ▶ | $self->processor( $self->default_processor ) unless $self->processor; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | |||||||||||
| 107 | 3 | my $output = $self->process_template; | |||||||||
| 108 | |||||||||||
| 109 | 2 | my $response = new OpenFrame::Response; | |||||||||
| 110 | 2 | $response->code( ofOK ); | |||||||||
| 111 | 2 | $response->message( $output ); | |||||||||
| 112 | |||||||||||
| 113 | 2 | return $response; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | ## override this method | ||||||||||
| 117 | 0 | ▶ | sub process_template { | ||||||||
|
Subroutine
process_template: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 118 | 0 | my $self = shift; | |||||||||
| 119 | 0 | throw OpenFrame::WebApp::Error::Abstract( class => ref($self) ); | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | ## what to do when template file is not found | ||||||||||
| 123 | 1 | ▶ | sub file_not_found { | ||||||||
|
Subroutine
file_not_found: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 124 | 1 | my $self = shift; | |||||||||
| 125 | 1 | throw OpenFrame::WebApp::Template::Error( | |||||||||
| 126 | flag => eTemplateNotFound, | ||||||||||
| 127 | template => $self->file, | ||||||||||
| 128 | ); | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | 7 | 1; | |||||||||
| 133 | |||||||||||
| 134 | __END__ | ||||||||||