/[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 40 by dpavlin, Mon Jun 30 20:02:12 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  use Template::Declare;  Template::Declare->init( roots => \@view_classes, around_template => sub {
 use Template::Declare::Tags; # defaults to 'HTML'  
 Template::Declare->init( roots => ['HTML','View'], around_template => sub {  
46          my ($orig, $path, $args, $code) = @_;          my ($orig, $path, $args, $code) = @_;
47            my $from = (caller(1))[3];
48            my $package = __PACKAGE__;
49            if ( $from !~ m/$package/ ) {
50                    warn "SKIP around_template for $path from $from\n";
51                    return $orig->();
52            } else {
53                    warn "WRAP around_temolate for $path from $from\n";
54            }
55    
56          my $t = time;          my $t = time;
57          html {          html {
58                  head {                  head {
59                          title { $path }                          title { $path }
60                          link {                          link {
61                                  { rel is 'stylesheet' }                                  { rel is 'stylesheet' }
62                                  { href is 'static/app.css' }                                  { href is '/static/app.css' }
63                                  { type is 'text/css' }                                  { type is 'text/css' }
64                                  { media is 'screen' }                                  { media is 'screen' }
65                          };                          };
66                          foreach my $js ( @javascript ) {                          foreach my $js ( @javascript ) {
67                                  script {                                  script {
68                                          { type is 'text/javascript' }                                          { type is 'text/javascript' }
69                                          { src is $js }                                          { src is "/$js" }
70                                  }                                  }
71                          }                          };
72                  }                  }
73                  body {                  body {
74                          $orig->();                          $orig->();
75                  }                  }
76          }          };
77          warn "TEMPLATE $path ",dump($args),sprintf(" in %.4fs\n",time - $t);          warn "TEMPLATE $path ",dump($args),sprintf(" in %.4fs\n",time - $t) if $debug;
78  });  });
79    
80  sub view {  =head2 page
81          my $self = shift;  
82          return Template::Declare->show( @_ );  Wrap template into html page
83    
84      Frey::HTML->page( 'template_name', $req, $args );
85    
86    =cut
87    
88    sub page {
89            my ( $self, $page, $req, $args ) = @_;
90            warn "## buffer_stack ",dump( Template::Declare->buffer_stack );
91            Template::Declare->buffer->clear; # XXX we need to manually do this!
92            warn "## page $page ",dump($args),"\n";
93            my $out = eval { Template::Declare->show( $page, $req, $args ) };
94            if ( $@ ) {
95                    carp "ERROR: $@";
96                    $out = Template::Declare->show( 'error', $req, "page $page " . dump($args) . ": $@" );
97            }
98            return $out;
99  }  }
100    
101    =head2 add_javascript
102    
103    Add javascript to current page
104    
105      Frey::HTML->add_javascript( 'static/javascript.js' );
106    
107    =cut
108    
109  sub add_javascript {  sub add_javascript {
110          my $self = shift;          my $self = shift;
111          my $js = shift or confess "no JavaScript path";          my $js = shift or confess "no JavaScript path";
# Line 52  sub add_javascript { Line 113  sub add_javascript {
113          warn "Added javascript $js\n";          warn "Added javascript $js\n";
114          push @javascript, $js;          push @javascript, $js;
115  }  }
116    
117    1;

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

  ViewVC Help
Powered by ViewVC 1.1.26