Play 2.0 - helper constructors in templates

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

+3
source share
1 answer

In the "constructor", I assume that you mean a list of arguments with different arguments. I don’t know the built-in way to do this, but I'm just starting to learn Play.

However, you can use the Enhance My Instance ™ template to achieve the same effect:

Using the to-do list example , let's say your template index.scala.htmlstarts:

@(tasks: List[Task], taskForm: Form[String])

In Application.scalayou call this with

  def tasks    = Action { Ok(views.html.index(Task.all(), taskForm)) }

If you want to leave a list of tasks:

  implicit def enhanceIndex(index: views.html.index.type) = new {
    def apply(f: Form[String]) = index(List.empty, f)
  }

Now you can call it like this:

  def tasks2   = Action { Ok(views.html.index(taskForm)) }

, , pimp-my-library .type, , views.html.index.

+2

All Articles