4

I'm trying to get my head around this concept, but the power of functional programming is yet weak in me, so I can't immediately get to grips with it. What I would like to do is define a re-usable template function with some parameters but an Html body as well.

For example, consider the following re-usable function in a template:

@myFunction(label: String, labelTarget: String, content: Html) = {
    <label for="@labelTarget">@label</label>
    <div>@content</div>
}

I would then like to use this function as follows:

...

<h2>My function content below!</h2>
@myFunction("label", "target") {
    <span>My additional content used by the function</span>
}

...

I get a feeling this should be doable, but can't seem to figure it out. Any help?

Pere Villega
  • 16,429
  • 5
  • 63
  • 100
tmbrggmn
  • 8,680
  • 10
  • 35
  • 44

1 Answers1

5

The only mistake you made is that you haven't defined your content as a new block of parameters :

@myFunction(label: String, labelTarget: String)(content: Html) = {
    <label for="@labelTarget">@label</label>
    <div>@content</div>
}
Maxime Dantec
  • 562
  • 2
  • 11
  • Thanks! I shall have to try this out when I get home. Could you perhaps provide a little more insight in how this works? – tmbrggmn Mar 15 '12 at 07:43
  • Having a looksy myself, these are apparently functions with [multiple parameter lists](http://docs.scala-lang.org/style/declarations.html#multiple_parameter_lists), which is sometimes confused with [function currying](http://stackoverflow.com/questions/6803211/what-is-a-curried-function-in-scala). I somehow managed to miss the multiple parameter lists bit so far :-) – tmbrggmn Mar 15 '12 at 08:15