How can I call a template with a variable number of arguments that have Html type in play?
I created a template in play2 defined like the following:
@(tabs: Html*)
<div class="btn-group" style="margin-bottom:20px">
@for((tab,index) <- tabs.zipWithIndex){
<a class="btn btn-mini btn-info active" id="display-hierarchy-@index" href="javascript:void(0)"><i class="icon icon-random icon-white"></i></a>
}
</div>
@for((tab,index) <- tabs.zipWithIndex){
<div id="display-hierarchy-tab-@index" class="onetab">
@tab
</div>
}
I tried to call it like
@views.html.tabs({
<a>tab1</a>
},{
<a>tab2</a>
})
I tried other varios combinations but it fails with:
type mismatch; found : scala.xml.Elem required: play.api.templates.Html
You can use a workaround:
Example call in a template file:
@TabsBuilder{
<a>tab1</a>
}{
<a>tab2</a>
}.map(tabs.apply)
The TabsBuilder:
package views.html
import play.api.templates.Html
class TabsBuilder(templates: Vector[Html]) {
def apply(html: Html) = new TabsBuilder(templates :+ html)
def map(f: Seq[Html] => Html) = f(templates)
}
object TabsBuilder {
def apply(html: Html) = new TabsBuilder(Vector(html))
}
The TabsBuilder enables you to write the code like you would have a variable number of parameter lists.