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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (show annotations)
Mon Jun 30 20:02:16 2008 UTC (14 years, 5 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 package Frey::HTML;
2
3 use strict;
4 use warnings;
5
6 use Time::HiRes qw/time/;
7 use Data::Dump qw/dump/;
8 use Carp qw/confess carp/;
9
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 );
41
42 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
50 my $t = time;
51 html {
52 head {
53 title { $title }
54 link {
55 { rel is 'stylesheet' }
56 { href is '/static/app.css' }
57 { type is 'text/css' }
58 { media is 'screen' }
59 };
60 foreach my $js ( @javascript ) {
61 script {
62 { type is 'text/javascript' }
63 { src is "/$js" }
64 }
65 };
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 "## page $page ",dump($args)," ",length($out)," bytes\n";
89 return scalar page_template($out);
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 {
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
108 1;

  ViewVC Help
Powered by ViewVC 1.1.26