[Bps-public-commit] r14352 - in Prophet/trunk: . lib/Prophet/Server

sartak at bestpractical.com sartak at bestpractical.com
Mon Jul 21 22:55:16 EDT 2008


Author: sartak
Date: Mon Jul 21 22:55:15 2008
New Revision: 14352

Modified:
   Prophet/trunk/   (props changed)
   Prophet/trunk/lib/Prophet/Server.pm
   Prophet/trunk/lib/Prophet/Server/View.pm

Log:
 r64702 at onn:  sartak | 2008-07-21 22:55:08 -0400
 Render templates from the web server


Modified: Prophet/trunk/lib/Prophet/Server.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server.pm	Mon Jul 21 22:55:15 2008
@@ -1,7 +1,11 @@
 package Prophet::Server;
+use strict;
+use warnings;
+use base 'HTTP::Server::Simple::CGI';
+
+use Prophet::Server::View;
 use Params::Validate qw/:all/;
 use JSON;
-use base 'HTTP::Server::Simple::CGI';
 
 sub prophet_handle {
     my $self = shift;
@@ -9,6 +13,12 @@
     return $self->{'_prophet_handle'};
 }
 
+sub new {
+    my $class = shift;
+    Template::Declare->init(roots => ['Prophet::Server::View']);
+    return $class->SUPER::new(@_);
+}
+
 sub handle_request {
     my ($self, $cgi) = validate_pos( @_, { isa => 'Prophet::Server'} ,  { isa => 'CGI' } );
     my $http_status;
@@ -25,6 +35,15 @@
     my ($cgi) = validate_pos( @_, { isa => 'CGI' } );
     my $p = $cgi->path_info;
 
+    if (Template::Declare->has_template($p)) {
+        my $content = Template::Declare->show($p);
+
+        return $self->_send_content(
+            content_type => 'text/html',
+            content      => $content,
+        );
+    }
+
     if ( $p =~ m|^/records\.json$| ) {
         $self->_send_content(
             content_type => 'text/x-json',

Modified: Prophet/trunk/lib/Prophet/Server/View.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server/View.pm	(original)
+++ Prophet/trunk/lib/Prophet/Server/View.pm	Mon Jul 21 22:55:15 2008
@@ -5,6 +5,14 @@
 use base 'Template::Declare';
 use Template::Declare::Tags;
 
+template '/' => sub {
+    html {
+        body {
+            h1 { "Welcome!" }
+        }
+    }
+};
+
 template record_table => sub {
     my $self = shift;
     my $records = shift;
@@ -12,7 +20,8 @@
     html {
         body {
             table {
-                for ( sort { $a->luid <=> $b->luid } $records->items ) {
+                my @items = $records ? $records->items : ();
+                for ( sort { $a->luid <=> $b->luid } @items ) {
                     my @atoms = $_->format_summary;
                     row {
                         cell { $_ } for @atoms;



More information about the Bps-public-commit mailing list