| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||
| 3 | # File: GX/View/Template/HTC.pm | ||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | package GX::View::Template::HTC; | ||
| 7 | |||
| 8 | use GX::Exception; | ||
| 9 | |||
| 10 | use HTML::Template::Compiled (); | ||
| 11 | |||
| 12 | |||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 14 | # Class setup | ||
| 15 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | use GX::Class; | ||
| 18 | |||
| 19 | extends 'GX::View::Template'; | ||
| 20 | |||
| 21 | build; | ||
| 22 | |||
| 23 | |||
| 24 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 25 | # Export mechanism | ||
| 26 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 27 | |||
| 28 | __PACKAGE__->_install_import_method; | ||
| 29 | |||
| 30 | |||
| 31 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 32 | # Private methods | ||
| 33 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 34 | |||
| 35 | sub _initialize_file_extensions { | ||
| 36 | |||
| 37 | return [ 'htc' ]; | ||
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | sub _initialize_options { | ||
| 42 | |||
| 43 | return { | ||
| 44 | 'cache' => 1 # enable in-memory caching | ||
| 45 | }; | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | sub _preload_template_file { | ||
| 50 | |||
| 51 | my $self = shift; | ||
| 52 | my $file = shift; | ||
| 53 | |||
| 54 | my $htc = eval { | ||
| 55 | HTML::Template::Compiled->new( | ||
| 56 | %{$self->_get_options}, | ||
| 57 | filename => $file, | ||
| 58 | cache => 1 | ||
| 59 | ); | ||
| 60 | }; | ||
| 61 | |||
| 62 | if ( ! $htc ) { | ||
| 63 | GX::Exception->throw( | ||
| 64 | message => "Cannot create the HTML::Template::Compiled instance", | ||
| 65 | subexception => $@ | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | my $result = $htc->output; | ||
| 70 | |||
| 71 | return; | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | sub _process_template_file { | ||
| 76 | |||
| 77 | my $self = shift; | ||
| 78 | my $file = shift; | ||
| 79 | my $parameters = shift; | ||
| 80 | my $options = shift; | ||
| 81 | |||
| 82 | my $htc = eval { | ||
| 83 | HTML::Template::Compiled->new( | ||
| 84 | %{$self->_get_options}, | ||
| 85 | ( $options ? %$options : () ), | ||
| 86 | filename => $file | ||
| 87 | ); | ||
| 88 | }; | ||
| 89 | |||
| 90 | if ( ! $htc ) { | ||
| 91 | GX::Exception->throw( | ||
| 92 | message => "Cannot create the HTML::Template::Compiled instance", | ||
| 93 | subexception => $@ | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | if ( $parameters ) { | ||
| 98 | $htc->param( $parameters ); | ||
| 99 | } | ||
| 100 | |||
| 101 | my $result = $htc->output; | ||
| 102 | |||
| 103 | return \$result; | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | sub _register { | ||
| 108 | |||
| 109 | my $self = shift; | ||
| 110 | my $application = shift; | ||
| 111 | |||
| 112 | $self->SUPER::_register( $application ); | ||
| 113 | |||
| 114 | my $options = $self->_get_options; | ||
| 115 | |||
| 116 | if ( ! exists $options->{'open_mode'} ) { | ||
| 117 | |||
| 118 | if ( defined $self->_get_template_encoding ) { | ||
| 119 | $options->{'open_mode'} = ':encoding(' . $self->_get_template_encoding . ')'; | ||
| 120 | } | ||
| 121 | |||
| 122 | } | ||
| 123 | |||
| 124 | if ( ! exists $options->{'path'} ) { | ||
| 125 | |||
| 126 | if ( defined $self->_get_templates_directory ) { | ||
| 127 | $options->{'path'} = [ $self->_get_templates_directory ]; | ||
| 128 | } | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | return; | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | 1; | ||
| 138 | |||
| 139 | __END__ |