Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.0 - auxiliary constructors in templates

Is it possible to have auxiliary constructors in Play 2.0 templates?

like image 863
noncom Avatar asked Nov 22 '25 14:11

noncom


1 Answers

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.

like image 106
Luigi Plinge Avatar answered Nov 25 '25 07:11

Luigi Plinge