4

Always i get a blank, i have a action in my controller like this

/**
 * @Route("/check/{key}.json", defaults={"_format"="json"})
 * @Template()
 */
public function processAction($upload_key)
{
  /* make thing */
  return array('data' => $process_data);
}

in my process.json.twig file i have

{% set res = { 'data': data } %}
{{ res | json_enconde | raw }}

other form

{{ { 'data': data } | json_enconde | raw }}

i've try this too:

{{ 'hello' | json_encode | raw }}

in chrome i get this response:

Connection:close
Content-Length:0
Content-Type:application/json
Date:Mon, 19 Dec 2011 05:13:17 GMT
Server:Apache/2.2.20 (Ubuntu)
X-Powered-By:PHP/5.3.6-13ubuntu3.3

and get nothing from the server, i cant solve this

rkmax
  • 17,633
  • 23
  • 91
  • 176

1 Answers1

5

There are two ways to achieve this, it depends on which you prefer and whether or not your action is supposed to support multiple _format types.

Option A - An action that only returns JSON

You can bypass the template completely.

In your controller remove the @Template annotation and instead return new Response(json_encode($process_data));

Option B - An action that supports different formats OR you just wish to render the JSON in a template

By an action that renders different formats I refer to an action with a route as so:

@Route("/check/{key}.{_format}", defaults={"_format"="json"}
@Template

Although a controller in this question goes down the route of "an action that only supports JSON but you want to render it in a template".

Assuming the controller's processAction returns return array('data' => $process_data); as the question asks then rendering this as JSON inside a template called process.json.twig should be done as follows {{ data|json_encode }}, there is no need to pre-process data or turn it into another array or anything like that inside the template.

Kasheen
  • 5,401
  • 2
  • 31
  • 41
  • @rkmax: No, I wouldn't say it's a bug. It's more efficient to just return the JSON data as the response from the controller anyway as Symfony doesn't need to do the templating stage. I'm not sure why it didn't call the template at all but perhaps it knows when `_format="json"` that it should just set content type as `application/json` and expect the appropriate data in a response. – Kasheen Dec 19 '11 at 19:51
  • anyway as I have a doubt, I've reported as bug to see what they tell me – rkmax Dec 19 '11 at 20:18
  • @rkmax: I did some more research with regards to TWIG and json_encode, I think you might be able to do `{{ data|json_encode }}` you may also have to filter through `raw` as well. In your above template code you were doing some pre-processing on `data` coming from the controller so possibly that is the issue. It only makes sense to use a template if you have `{_format}` as part of your route (`@Route("/check/{key}.{_format}", defaults={"_format"="json"})`) as well though if you want one action to support multiple formats. – Kasheen Dec 19 '11 at 20:30
  • was so in fact as had at first, and it's more like as 1.x working in Symfony – rkmax Dec 20 '11 at 00:11
  • @rkmax: I'm sorry, I think something got last in translation there ;). Did `{{ data|json_encode }}` work as well? – Kasheen Dec 20 '11 at 02:16
  • Cool, I'll add it to the answer for anyone else who looks. – Kasheen Dec 21 '11 at 18:36
  • Excelente! i finally complete with [this](http://stackoverflow.com/a/8593471/816721) – rkmax Dec 21 '11 at 21:02