3

I am working with play 2.0 with java about a month and there is one thing that I really cannot understand. How templates are really work? What is the best way of passing params to them?

What's the difference between

@(name: String, value: String)

and

@(name: String)(value: String)

Is this only for convenience?

I found this question which lifts the veil of secrecy, but it doesn't tell which way I should choose.

Community
  • 1
  • 1
Alex Povar
  • 4,890
  • 3
  • 29
  • 44

2 Answers2

4

canonical example would be:

// main.scala.html

@(title: String)(content: Html)
....

// index.scala.html

@main("Foo Title") {
  <div>this content Html param passed in as a block {}</div>
}

with: @(title: String, content: Html)

the syntax is not quite as nice:

@main("Foo Title", {
  <div>...</div>
})
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
  • And one more question: are {} brackets for HTML only? – Alex Povar May 31 '12 at 03:32
  • @AlexPovar in this case, well, yes, method argument is of type Html ;-) You can apply this anywhere, not just template layer, it's part of the Scala language itself; if you have def foo(a: Int)(b: Unit) = b, you can call it like foo(1) { println("bar") } – virtualeyes May 31 '12 at 07:10
3

In the first case you are passing multiple parameters to a function. In the second case you are using currying. According to Wikipedia:

In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application). http://en.wikipedia.org/wiki/Currying

What is best differs per use case.

Edit: note that templates are just Scala functions.

Leonard Punt
  • 1,051
  • 9
  • 17