/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (hide annotations)
Mon Jun 30 20:02:16 2008 UTC (14 years, 7 months ago) by dpavlin
File size: 2100 byte(s)
 r44@eeepy:  dpavlin | 2008-06-30 18:08:33 +0200
 Change logic of generating html page using Frey::HTML->page
 because Template::Declare around_template is doing it around
 *all* templates (including those used with show).

1 dpavlin 23 package Frey::HTML;
2 dpavlin 10
3     use strict;
4     use warnings;
5    
6     use Time::HiRes qw/time/;
7     use Data::Dump qw/dump/;
8 dpavlin 26 use Carp qw/confess carp/;
9 dpavlin 19
10 dpavlin 40 #use Template::Declare;
11     use base qw/Template::Declare/;
12     use Template::Declare::Tags; # defaults to 'HTML'
13    
14 dpavlin 23 =head1 NAME
15    
16     Frey::HTML - generate html pages
17    
18     =head2 METHODS
19    
20     =cut
21    
22 dpavlin 19 # FIXME
23 dpavlin 10
24 dpavlin 21 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 dpavlin 40 # this will import templates as fully qualified paths
37     import_templates $_ under "/$_/";
38 dpavlin 21 }
39    
40 dpavlin 10 warn "available templates = ",dump( Template::Declare->templates );
41    
42 dpavlin 14 our @javascript;
43 dpavlin 23 our $debug = 0;
44 dpavlin 14
45 dpavlin 42 Template::Declare->init( roots => \@view_classes );
46     sub page_template {
47     my ( $body ) = @_;
48     my $title = (caller(0))[3];
49 dpavlin 38
50 dpavlin 10 my $t = time;
51     html {
52 dpavlin 14 head {
53 dpavlin 42 title { $title }
54 dpavlin 14 link {
55     { rel is 'stylesheet' }
56 dpavlin 26 { href is '/static/app.css' }
57 dpavlin 14 { type is 'text/css' }
58     { media is 'screen' }
59     };
60     foreach my $js ( @javascript ) {
61     script {
62     { type is 'text/javascript' }
63 dpavlin 26 { src is "/$js" }
64 dpavlin 14 }
65 dpavlin 32 };
66 dpavlin 14 }
67 dpavlin 42 body { outs_raw $body }
68 dpavlin 23 };
69 dpavlin 42 };
70 dpavlin 10
71 dpavlin 23 =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 dpavlin 32 my ( $self, $page, $req, $args ) = @_;
81 dpavlin 39 warn "## buffer_stack ",dump( Template::Declare->buffer_stack );
82     Template::Declare->buffer->clear; # XXX we need to manually do this!
83 dpavlin 32 my $out = eval { Template::Declare->show( $page, $req, $args ) };
84 dpavlin 26 if ( $@ ) {
85     carp "ERROR: $@";
86 dpavlin 32 $out = Template::Declare->show( 'error', $req, "page $page " . dump($args) . ": $@" );
87 dpavlin 26 }
88 dpavlin 42 warn "## page $page ",dump($args)," ",length($out)," bytes\n";
89     return scalar page_template($out);
90 dpavlin 10 }
91 dpavlin 14
92 dpavlin 23 =head2 add_javascript
93    
94     Add javascript to current page
95    
96     Frey::HTML->add_javascript( 'static/javascript.js' );
97    
98     =cut
99    
100 dpavlin 14 sub add_javascript {
101     my $self = shift;
102     my $js = shift or confess "no JavaScript path";
103     # return unless -e $js; # FIXME
104     warn "Added javascript $js\n";
105     push @javascript, $js;
106     }
107 dpavlin 39
108     1;

  ViewVC Help
Powered by ViewVC 1.1.26