Difference between revisions of "User:Exor674/RoutingExamplePages"

From Dreamwidth Notes
Jump to: navigation, search
(Created page with '== Not using a template -- Data Page == <source lang="perl"> package DW::Controller::MOO; use strict; use warnings; use DW::Routing; use DW::Request; use JSON; DW::Routing->reg…')
(No difference)

Revision as of 02:50, 5 November 2009

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;