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