Is it possible to have auxiliary constructors in Play 2.0 templates?
By "constructor" I assume you mean argument list with different arguments. I don't know of a built-in way to do this, but I've only just starting learning Play.
However you can use the Enhance My Instance™ pattern to achieve the same effect:
Using the to-do list example, say your index.scala.html template begins:
@(tasks: List[Task], taskForm: Form[String])
In Application.scala you call this with
def tasks = Action { Ok(views.html.index(Task.all(), taskForm)) }
If you want to leave out the task list:
implicit def enhanceIndex(index: views.html.index.type) = new {
def apply(f: Form[String]) = index(List.empty, f)
}
Now you can call it thus:
def tasks2 = Action { Ok(views.html.index(taskForm)) }
This is essentially just the pimp-my-library pattern using .type to narrow the scope to a particular instance, in this case the views.html.index object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With