/[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 38 - (show annotations)
Mon Jun 30 20:02:08 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 2093 byte(s)
 r40@eeepy:  dpavlin | 2008-06-30 13:37:18 +0200
 Move item browser into Strix::View
 
 We still have problem with Template::Declare executing multiple times

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

  ViewVC Help
Powered by ViewVC 1.1.26