User:Exor674/RoutingExamplePages

From Dreamwidth Notes
< User:Exor674
Revision as of 02:50, 5 November 2009 by Exor674 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Not using a template -- Data Page

package DW::Controller::MOO;
use strict;
use warnings;
use DW::Routing;
use DW::Request;
use JSON;
 
DW::Routing->register_string(  "/moo", \&handler );
 
sub handler {
    my $r = DW::Request->get;
 
    $response = {
        time => time,
        cute => ["kittens", "puppies"],
        string => "Kittens and puppies are cute",
    };
 
    my $formats = {
        'json' => [ "application/json", sub { $_[0]->print(objToJson( $_[1] )); } ],
        'text' => [ "text/plain" sub { $_[0]->print( $_[1]->string )) } ],
    };
 
    my $format = $formats->{ $_[0]->format || 'json' };
 
    return $r->NOT_FOUND if ( ! $format );
 
    $r->content_type( $format->[0] ) if $format->[0];
 
    $format->[1]->( $r, $response );
 
    return $r->OK;
}
 
1;