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

Legend:
Removed from v.14  
changed lines
  Added in v.42

  ViewVC Help
Powered by ViewVC 1.1.26