[Dancer-users] using a template to create a JSON structure to send back to an ajax request

Matthew Vickers mvickers at quispiam.com
Tue Jan 18 11:22:48 CET 2011


On 18/01/11 1:05 PM, Puneet Kishor wrote:
> As the subject line says, I would like to use a template to create a 
> JSON structure that I would like to send back to the browser in 
> response to an ajax request. Is that possible?
>
>
Hi Puneet,

I believe so, the following code snippet works for me.

sub json {
   return 'application/json';
}

get '/testjson' => sub {
   content_type json;
   return template 'json.tt', { r => { id => 1, name => "Widgets Co."} 
}, { layout => undef };
};

Where json.tt is

{
     "name": "<% r.name %>",
     "id":<% r.id %>
}

Cheers

Matt


More information about the Dancer-users mailing list