7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
use Carp qw/confess carp/; |
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 |
=head1 NAME |
15 |
|
|
16 |
Frey::HTML - generate html pages |
Frey::HTML - generate html pages |
23 |
|
|
24 |
our @view_classes = qw( |
our @view_classes = qw( |
25 |
Frey::View |
Frey::View |
|
Frey::REST |
|
26 |
Strix::View |
Strix::View |
27 |
); |
); |
28 |
|
|
33 |
$path =~ s!::!/!g; |
$path =~ s!::!/!g; |
34 |
$path .= '.pm'; |
$path .= '.pm'; |
35 |
require $path or warn "Can't require $_ from $path: $!"; |
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 ); |
42 |
our @javascript; |
our @javascript; |
43 |
our $debug = 0; |
our $debug = 0; |
44 |
|
|
45 |
use Template::Declare; |
Template::Declare->init( roots => \@view_classes ); |
46 |
use Template::Declare::Tags; # defaults to 'HTML' |
sub page_template { |
47 |
Template::Declare->init( roots => \@view_classes, around_template => sub { |
my ( $body ) = @_; |
48 |
my ($orig, $path, $args, $code) = @_; |
my $title = (caller(0))[3]; |
49 |
|
|
50 |
my $t = time; |
my $t = time; |
51 |
html { |
html { |
52 |
head { |
head { |
53 |
title { $path } |
title { $title } |
54 |
link { |
link { |
55 |
{ rel is 'stylesheet' } |
{ rel is 'stylesheet' } |
56 |
{ href is '/static/app.css' } |
{ href is '/static/app.css' } |
62 |
{ type is 'text/javascript' } |
{ type is 'text/javascript' } |
63 |
{ src is "/$js" } |
{ src is "/$js" } |
64 |
} |
} |
65 |
} |
}; |
|
} |
|
|
body { |
|
|
$orig->(); |
|
66 |
} |
} |
67 |
|
body { outs_raw $body } |
68 |
}; |
}; |
69 |
warn "TEMPLATE $path ",dump($args),sprintf(" in %.4fs\n",time - $t) if $debug; |
}; |
|
}); |
|
70 |
|
|
71 |
=head2 page |
=head2 page |
72 |
|
|
77 |
=cut |
=cut |
78 |
|
|
79 |
sub page { |
sub page { |
80 |
my $self = shift; |
my ( $self, $page, $req, $args ) = @_; |
81 |
warn "## page ",dump($_[0],ref($_[1]),$_[2]),"\n"; |
warn "## buffer_stack ",dump( Template::Declare->buffer_stack ); |
82 |
my $page = eval { Template::Declare->show( @_ ) }; |
Template::Declare->buffer->clear; # XXX we need to manually do this! |
83 |
|
my $out = eval { Template::Declare->show( $page, $req, $args ) }; |
84 |
if ( $@ ) { |
if ( $@ ) { |
85 |
carp "ERROR: $@"; |
carp "ERROR: $@"; |
86 |
$page = Template::Declare->show( 'error', $_[1], "page" . dump($_[0], ref($_[1])) . "\n$@" ); |
$out = Template::Declare->show( 'error', $req, "page $page " . dump($args) . ": $@" ); |
|
warn $page; |
|
87 |
} |
} |
88 |
return $page; |
warn "## page $page ",dump($args)," ",length($out)," bytes\n"; |
89 |
|
return scalar page_template($out); |
90 |
} |
} |
91 |
|
|
92 |
=head2 add_javascript |
=head2 add_javascript |
104 |
warn "Added javascript $js\n"; |
warn "Added javascript $js\n"; |
105 |
push @javascript, $js; |
push @javascript, $js; |
106 |
} |
} |
107 |
|
|
108 |
|
1; |