X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FGruntmaster%2FPage%2FBase.pm;h=89f8478e6c62ce5cfa9c01cd7dd542ed0a66e911;hb=9da2f4958089ae517865b808be61bb12d43b61de;hp=cf7d2291587809652e8636c03d692692161ed4ae;hpb=7e3d8247f03853c8fd28a48dcd4681a02df32cdd;p=plack-app-gruntmaster.git diff --git a/lib/Gruntmaster/Page/Base.pm b/lib/Gruntmaster/Page/Base.pm index cf7d229..89f8478 100644 --- a/lib/Gruntmaster/Page/Base.pm +++ b/lib/Gruntmaster/Page/Base.pm @@ -9,12 +9,34 @@ use HTML::Template::Compiled; ################################################## +sub read_templates { + my $root = 'tmpl'; + my $name = shift; + + map { m/\.(.+)$/; $1 => scalar read_file $_ } ; +} + +my %header_templates = read_templates 'header'; +my %footer_templates = read_templates 'footer'; + +sub header{ + my ($language, $title) = @_; + $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; +} + +sub footer{ + $footer_templates{$_[0]}; +} + +################################################## + use POSIX (); use Gruntmaster::Data (); use List::Util (); use LWP::UserAgent; my $ua = LWP::UserAgent->new; +my %templates; sub import { my $caller = caller; @@ -25,8 +47,6 @@ sub import { no strict 'refs'; *{"${caller}::strftime"} = \&POSIX::strftime; - *{"${caller}::NAME"} = sub () { $name }; - *{"${caller}::TITLE"} = sub () { $title }; *{"${caller}::debug"} = sub { local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) @@ -37,112 +57,19 @@ sub import { my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]"); $ua->request($req) }; -} - -################################################## -my %orig_header_templates = ( - en => <<'HTML', - -TITLE_GOES_HERE - - - - - - - - -
- -

TITLE_GOES_HERE

-
-HTML -); - -my %orig_footer_templates = ( - en => <<'HTML', - - -HTML -); - -sub patch_templates { - my $root = 'tmpl'; - return %{$_[0]} unless -d $root; - my ($templates, $name) = @_; - my %out = %$templates; - for (<$root/$name.*>) { - m/\.(.+)$/; - $out{$1} = read_file $_ + if ($name) { + $templates{$caller} = { read_templates $name }; + $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller}; + $templates{$caller}{$_} .= footer $_ for keys $templates{$caller}; } - - %out -} - -my %header_templates = patch_templates \%orig_header_templates, 'header'; -my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; - -sub header{ - my ($language, $title) = @_; - $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; -} - -sub footer{ - $footer_templates{$_[0]}; -} - -sub cook_templates { - my ($templates, $name, $title) = @_; - - my %out = patch_templates $templates, $name; - $out{$_} = header ($_, $title) . $out{$_} for keys %out; - $out{$_} .= footer $_ for keys %out; - - %out } ################################################## -my %templates; - sub generate{ my ($self, $lang, @args) = @_; - $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self}; - my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',); $self->_generate($htc, $lang, @args); my $out = $htc->output; @@ -155,7 +82,7 @@ sub _generate {} sub max_age { 60 } sub variants { - [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ] + [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ] } 1