/[Frey]/trunk/lib/Frey/HTML.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/HTML.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/lib/HTML.pm revision 10 by dpavlin, Sat Jun 28 22:13:05 2008 UTC trunk/lib/Frey/HTML.pm revision 32 by dpavlin, Sun Jun 29 20:52:33 2008 UTC
# Line 1  Line 1 
1  package HTML;  package Frey::HTML;
2    
3  use strict;  use strict;
4  use warnings;  use warnings;
5    
6  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use View;  use Carp qw/confess carp/;
9    
10  use Data::Dump qw/dump/;  =head1 NAME
11    
12    Frey::HTML - generate html pages
13    
14    =head2 METHODS
15    
16    =cut
17    
18    # FIXME
19    
20    our @view_classes = qw(
21            Frey::View
22            Strix::View
23    );
24    
25    warn "Using view classes ", dump( @view_classes );
26    
27    foreach ( @view_classes ) {
28            my $path = $_;
29            $path =~ s!::!/!g;
30            $path .= '.pm';
31            require $path or warn "Can't require $_ from $path: $!";
32    }
33    
34  warn "available templates = ",dump( Template::Declare->templates );  warn "available templates = ",dump( Template::Declare->templates );
35    
36    our @javascript;
37    our $debug = 0;
38    
39  use Template::Declare;  use Template::Declare;
40  use Template::Declare::Tags; # defaults to 'HTML'  use Template::Declare::Tags; # defaults to 'HTML'
41  Template::Declare->init( roots => ['HTML','View'], around_template => sub {  Template::Declare->init( roots => \@view_classes, around_template => sub {
42          my ($orig, $path, $args, $code) = @_;          my ($orig, $path, $args, $code) = @_;
43          my $t = time;          my $t = time;
44          html {          html {
45                  head {}                  head {
46                            title { $path }
47                            link {
48                                    { rel is 'stylesheet' }
49                                    { href is '/static/app.css' }
50                                    { type is 'text/css' }
51                                    { media is 'screen' }
52                            };
53                            foreach my $js ( @javascript ) {
54                                    script {
55                                            { type is 'text/javascript' }
56                                            { src is "/$js" }
57                                    }
58                            };
59                    }
60                  body {                  body {
61                          $orig->();                          $orig->();
62                  }                  }
63          }          };
64          warn "TEMPLATE $path ",dump($args),sprintf(" in %.4fs\n",time - $t);          warn "TEMPLATE $path ",dump($args),sprintf(" in %.4fs\n",time - $t) if $debug;
65  });  });
66    
67  sub view {  =head2 page
68  #       warn "## view",dump( @_ );  
69    Wrap template into html page
70    
71      Frey::HTML->page( 'template_name', $req, $args );
72    
73    =cut
74    
75    sub page {
76            my ( $self, $page, $req, $args ) = @_;
77            warn "## page $page ",dump($args),"\n";
78            my $out = eval { Template::Declare->show( $page, $req, $args ) };
79            if ( $@ ) {
80                    carp "ERROR: $@";
81                    $out = Template::Declare->show( 'error', $req, "page $page " . dump($args) . ": $@" );
82            }
83            return $out;
84    }
85    
86    =head2 add_javascript
87    
88    Add javascript to current page
89    
90      Frey::HTML->add_javascript( 'static/javascript.js' );
91    
92    =cut
93    
94    sub add_javascript {
95          my $self = shift;          my $self = shift;
96          return Template::Declare->show( @_ );          my $js = shift or confess "no JavaScript path";
97    #       return unless -e $js; # FIXME
98            warn "Added javascript $js\n";
99            push @javascript, $js;
100  }  }

Legend:
Removed from v.10  
changed lines
  Added in v.32

  ViewVC Help
Powered by ViewVC 1.1.26