3

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
Schleichardt
  • 7,502
  • 1
  • 27
  • 37
raisercostin
  • 8,777
  • 5
  • 67
  • 76
  • 2
    Maybe duplicate to http://stackoverflow.com/questions/13859600/how-to-call-a-template-which-accepts-variable-number-of-args-in-play-framework-2 – Schleichardt Sep 22 '13 at 21:54

1 Answers1

1

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.

Schleichardt
  • 7,502
  • 1
  • 27
  • 37