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

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

  ViewVC Help
Powered by ViewVC 1.1.26