on render(...) and other text content types

199 views
Skip to first unread message

Derek Mortimer

unread,
Oct 27, 2009, 10:07:35 AM10/27/09
to play-framework
So under the scenario where I have

class Application extends Controller
{
public static void index()
{
render();
}
}

the mapping for that is then to lookup '/app/views/Application/
index.html' and render using the templating engine. Are we limited to
only using the templating engine on html files in this way, or can I
define other files like index.xml and have them served up using render
(...) with the correct content-type?

I understand there are renderXML and renderJSON methods, but it would
be very nice to be able to use the templating engine for XML
generation.

In the case of multiple template files existing for an action (e.g.
html + xml + txt) the framework could choose to respect the accepts
header or fall back onto some app specific configuration.

Any thoughts on this, or ways I could make play render XML using the
templating engine?

Guillaume Bort

unread,
Oct 27, 2009, 10:17:02 AM10/27/09
to play-fr...@googlegroups.com
Yes if you send your request with

Accept: text/xml

The render method should automatically switch to index.xml (same for
index.txt and index.json).

Moreover you should find the format in the request.format attribute.

Tristan Wehrmaker

unread,
Oct 27, 2009, 10:40:33 AM10/27/09
to play-fr...@googlegroups.com
Hi!

First of all, I think Play! is a really nice Framework.

It would be nice, if it is possible to render every content type with an
appropriate template.

For example: If the systems gets a request with "Accept: text/x-vcard"
than it would look for the template index.vcf or something like this.

I think html, xml and json are not the only content types, that should
be handled in terms of REST. For a RESTful Architecture it would be
really nice to handle as many representations as possible.

Is there already a way to do this type of content-negotiation in Play!?
What would be the best way to do this? Are there any other content-types
render() can work with? Is it possible to define new mappings (requested
content-type --> template)?

Greetings, Tristan||

Guillaume Bort schrieb:

Guillaume Bort

unread,
Oct 27, 2009, 10:40:39 AM10/27/09
to play-fr...@googlegroups.com
If fact if the Accept header is not suitable for you, you can even use
different URL path. For example, in conf/routes:

GET /index Application.index
GET /index.xml Application.index(format:'xml')

You can even extract the format information, dynamically from the URL path

GET /index.{format} Application.index

Guillaume Bort

unread,
Oct 27, 2009, 10:55:36 AM10/27/09
to play-fr...@googlegroups.com
Currently the only content type resolved by the engine itself are only
txt,html,json and xml.
But I agree it's lame, and surely it should be pluggable.

However you can easily do it yourself either by specifying the correct
format value for a special URL in the route file, or using a @Before
interceptor that examine the Accept header:

@Before
static void setFormat() {
if(request.headers.get("accept").value().equals("text/v-card")) {
request.format = "vcf";
}
}

And it will finally render the index.vcf template and set
automatically response mime-type to text/v-card.

Derek Mortimer

unread,
Oct 27, 2009, 10:56:33 AM10/27/09
to play-framework
Fantastic, thanks for this!

On Oct 27, 2:40 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> If fact if the Accept header is not suitable for you, you can even use
> different URL path. For example, in conf/routes:
>
> GET    /index               Application.index
> GET    /index.xml         Application.index(format:'xml')
>
> You can even extract the format information, dynamically from the URL path
>
> GET    /index.{format}   Application.index
>
> On Tue, Oct 27, 2009 at 3:17 PM, Guillaume Bort
>
>
>
> <guillaume.b...@gmail.com> wrote:
> > Yes if you send your request with
>
> > Accept: text/xml
>
> > The render method should automatically switch to index.xml (same for
> > index.txt and index.json).
>
> > Moreover you should find the format in the request.format attribute.
>
Reply all
Reply to author
Forward
0 new messages