Reputation: 415
I've been reading the documentation on the Play! website and can't see where it explains what parameter groups are and what they would be used for.
What is the difference between:
@(title: String)(user: User)
And:
@(title: String, user: User)
If someone could help me out here it'd be greatly appreciated.
Upvotes: 3
Views: 511
Reputation: 8901
As @mgosk suggests in the comments, multiple parameter lists are a standard feature of Scala functions and Google can answer that much better than I could.
With regard to specifically Play templates however, they're exceedingly useful.
TLDR Parameter groups in Play templates are useful for:
One reason you might use them is form passing implicit parameters to views. Implicit parameters are added to the call site by the compiler, and as the spec points out they must be marked as such in the last (or only) argument list, e.g:
@(title: String)(implicit request: RequestHeader)
<h1>@title</h1>
The path is for this request is <b>@request.path</b>.
When calling that template from an action you don't have to explicitly provide the request header as long as there's one in scope (see here for more details on that).
Another really useful use for multiple parameter lists in templates is for "wrapping" content, especially given that Scala functions can be invoked with braces instead of parenthesis. Say you have some partial templates which represent, say, several different widgets, but those templates are always surrounded by the same boilerplate HTML. You can then create a template wrapper (called widgetWrapper.scala.html
) like this:
@(name: String)(details: Html)
<div class="item">
<div class="item-name">@name</div>
<div class="item-body">
@details
</div>
</div>
This can be invoked like so:
@(item: WidgetA)
@widgetWrapper(item.name) {
<ul class="item-details">
<li>@item.detail1</li>
<li>@item.detail2</li>
</ul>
}
This last technique is how one defines standardized "page chrome" or a site, e.g (file standardLayout.scala.html
):
@(title: String)(content: Html)(sidebar: Html)
<html>
<head>
<title>@title</title>
</head>
<body>
<header>
<h1>@title</h1>
</header>
<article>@content</article>
<aside>@sidebar</aside>
</body>
</html>
used like so:
@()
@standardLayout("My Page on Goats") {
<p>
Lots of text about goats...
</p>
} {
<ul class="sidebar-links">
<li><a href="https://en.wikipedia.org/wiki/Goat">More about goats!</li>
</ul>
}
As you can see, the content in the two brace-delimited sections are passed as Twirl Html
to the layout template as, respectively, the main content and the sidebar (with the title passed in the first argument as a string.)
Such handiness is made possible by multiple parameter groups.
Upvotes: 2