Changeset 23317
- Timestamp:
- 11/12/08 14:23:36 (5 years ago)
- Location:
- lang/perl/NanoA/trunk
- Files:
-
- 1 added
- 2 modified
-
example/MyApp/copyright.mt (added)
-
example/MyApp/mojo.mt (modified) (1 diff)
-
lib/NanoA.pm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/NanoA/trunk/example/MyApp/mojo.mt
r23309 r23317 5 5 <body> 6 6 Hello to <%= $app->query->param('user') || '' %>. 7 <hr> 8 <%= $app->include('copyright') %> 7 9 </body> 8 10 </html> -
lang/perl/NanoA/trunk/lib/NanoA.pm
r23310 r23317 140 140 return 141 141 unless -e "$path.mt"; 142 NanoA::Mojo::Template->__load($path); 143 } 144 145 sub not_found { 146 my ($klass, $opts) = @_; 147 $opts->{not_found} || 'NanoA/NotFound'; 148 } 149 150 sub camelize { 151 # copied from String::CamelCase by YAMANSHINA Hio 152 my $s = shift; 153 join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s)); 154 } 155 156 package NanoA::Mojo::Template; 157 158 use strict; 159 use warnings; 160 161 use base qw(NanoA); 162 163 my $MOJO_LOADED; 164 165 sub include { 166 my ($app, $path) = @_; 167 my $module = $app->__load($app->config->{prefix} . "/$path"); 168 $module->__run_as($app); 169 } 170 171 sub __load { 172 my ($self, $path) = @_; 173 my ($module, $code) = $self->__compile($path); 174 local $@; 175 eval $code; 176 die $@ if $@; 177 $module; 178 } 179 180 sub __compile { 181 my ($self, $path) = @_; 142 182 my $module = $path; 143 183 $module =~ s{/}{::}; 144 require 'Mojo/Template.pm'; 184 unless ($MOJO_LOADED) { 185 require 'Mojo/Template.pm'; 186 $MOJO_LOADED = 1; 187 } 145 188 my $mt = Mojo::Template->new; 146 $mt->parse( read_file("$path.mt"));189 $mt->parse(__read_file("$path.mt")); 147 190 $mt->build(); 148 191 my $code = $mt->code(); 149 192 $code = << "EOT"; 150 use Mojo::Template;151 193 package $module; 152 use base qw(NanoA );194 use base qw(NanoA::Mojo::Template); 153 195 sub run { 154 196 my \$app = shift; … … 156 198 \$code->(); 157 199 } 200 sub __run_as { 201 my (\$klass, \$app) = \@_; 202 run(\$app); 203 } 158 204 1; 159 205 EOT 160 206 ; 161 local $@; 162 eval $code; 163 die $@ if $@; 164 $module; 165 } 166 167 sub not_found { 168 my ($klass, $opts) = @_; 169 $opts->{not_found} || 'NanoA/NotFound'; 170 } 171 172 sub camelize { 173 # copied from String::CamelCase by YAMANSHINA Hio 174 my $s = shift; 175 join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s)); 176 } 177 178 sub read_file { 207 ($module, $code); 208 } 209 210 sub __read_file { 179 211 my $fname = shift; 180 212 open my $fh, '<', $fname or die "cannot read $fname:$!";
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)