Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JSON templates in Play 2.0

I'm getting this error:

Compilation error [package views.json.Runs does not exist]

when it clearly does exist. I can't figure out what I could be doing wrong.

the action in the Runs controller:

@BodyParser.Of(play.mvc.BodyParser.Json.class)
public static Result view(Long task_id, Long run_id) {
    Run run = Run.find.byId(run_id);
    return ok(views.json.Runs.view.render(run));
}

app/views/Runs/view.scala.json:

@(run: Run)

{
    "started": "@run.started",
    "completed": "@run.completed"
}

I've gotten a few html templates working, but this is the first JSON template I've tried with 2.0. I'm not sure what else to try since it's already about as basic as possible. Anyone have any ideas?

Update: I've gotten several suggestions for workarounds, but I'm still interested in knowing how to get the templates working, if only to better understand the changes to 2.0.

like image 352
Brad Mace Avatar asked Jun 29 '12 20:06

Brad Mace


2 Answers

Only html, xml and txt appear to be supported by default. For other file extensions and formats, you'd have to register additional "templateTypes" in $PLAY_HOME/framework/src/sbt-plugin/src/main/scala/PlaySettings.scala (see also: SBT Settings, near the bottom).

It may be helpful to look at the standard template types definitions which are in $PLAY_HOME/framework/src/play/src/main/scala/play/api/templates/Templates.scala.

You could also cheat and serve your json from a txt file, but do response().setContentType("application/json") before calling the render method.

like image 156
ejain Avatar answered Sep 25 '22 16:09

ejain


For Json, why don't you directly produce a Json string using the Json helper:

public static Result view(Long task_id, Long run_id) {
    Run run = Run.find.byId(run_id);
    return ok(play.libs.Json.toJson(run));
}
like image 38
ndeverge Avatar answered Sep 23 '22 16:09

ndeverge