tmbrggmn
tmbrggmn

Reputation: 8830

Play 2.0 re-usable template function with HTML body

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?

Upvotes: 4

Views: 832

Answers (1)

Maxime Dantec
Maxime Dantec

Reputation: 562

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>
}

Upvotes: 5

Related Questions