How do you separate the rendering concerns from the controller?

Asked by snoopy3368

I'm not sure if my question violates Play! architecture or is inline with it, or that I don't understand how to achieve a desired result.

Basically, I want to create an app that can render what the client prefers. So if the client prefers HTML, that can be returned, if the client prefers JSON or XML etc. that can be arranged. I.e. the controller doesn't care. The controller receives input, manipulates the Model and then selects those parts of the model that should be rendered and possibly the flow, and then calls Play! to select the proper rendering.

In other words I love how Play! can figure out how to make it all work by using simple conventions. In this case, the selection of the rendering seems to be left to the Controller to figure out. Is this by design? Or is this my lack of understanding in how to make Play! work?

Question information

Language:
English Edit question
Status:
Solved
For:
play framework Edit question
Assignee:
No assignee Edit question
Solved by:
Leo
Solved:
Last query:
Last reply:
Revision history for this message
Best Leo (lwe-zen) said :
#1

Hi,

In fact, Play allready provides multiple response types using the
templates solution.
For instance, suppose you want to offer HTML and JSON response type to
a/ ShoppingCart.content /action :

public static void content( String id ) {
    ShoppingCart shoppingCart = ...get the cart here
    render( shoppingCart );
}

You can define two views : ShoppingCart/content.html and
ShoppingCart/content.json, each providing a view of the shopping cart in
the desired format.
The ShoppingCart/content.json view may be constructed explicitely :
{
   "items": #{list items:shoppingCart.items, as:'item' }
    ... display the item here ...
    #{/list},
    ... other shoppingCart infos here ...
   "lastModified": ${shoppingCart.lastModified}
}

Or it may just proxy the json support class to render the cart as JSON:

${fr.zenexity.json.JSON.toJSON( shoppingCart ) }

Use the later solution only if the ShoppingCart object can to be *fully*
returned to the client.

Of course, we agree there are cases where it would be easier to render
JSON/ XML content without defining a template. However, in many cases,
model objects need to be adapted or filtered in some ways or others
before being sent back to the client. In these cases templates provide a
flexible solution.

Hope this helps,

Leo

snoopy3368 wrote:
> New question #52346 on play framework:
> https://answers.launchpad.net/play/+question/52346
>
> I'm not sure if my question violates Play! architecture or is inline with it, or that I don't understand how to achieve a desired result.
>
> Basically, I want to create an app that can render what the client prefers. So if the client prefers HTML, that can be returned, if the client prefers JSON or XML etc. that can be arranged. I.e. the controller doesn't care. The controller receives input, manipulates the Model and then selects those parts of the model that should be rendered and possibly the flow, and then calls Play! to select the proper rendering.
>
> In other words I love how Play! can figure out how to make it all work by using simple conventions. In this case, the selection of the rendering seems to be left to the Controller to figure out. Is this by design? Or is this my lack of understanding in how to make Play! work?
>
>
>
>

Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#2

Hmmm, I can't find the code that chooses the template from the accept header anymore. I guess it was lost from stable-2 to stable-3.

Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#3

This should be fixed in r210

Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#4

Hi,

There's a sample app showing how you can force a particular render or let Play! decide which template (json/xml/etc) to use. See http://www.playframework.org/manual/contents/templateselection

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#5

Thanks Leo, that solved my question.

Revision history for this message
snoopy3368 (snoopy3368+launchpad) said :
#6

BTW, the sample-jxon-xml app will not work unless you have r217 or higher.