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

Puneet Kishor punk.kish at gmail.com
Tue Jan 18 16:13:41 CET 2011



Matthew Vickers wrote:
> 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 %>
> }
>


Oh! Brilliant! That is exactly what I was looking for. I am going to try 
it out and report back.

Many thanks,

Puneet.


More information about the Dancer-users mailing list