Play2 invocation template with variable arguments of type Html

How can I call a template with a variable number of arguments with an Html type in the game?

I created a template in play2 defined as follows:

@(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 calling him like

 @views.html.tabs({ <a>tab1</a> },{ <a>tab2</a> }) 

I tried other varios combinations, but with an error:

 type mismatch; found : scala.xml.Elem required: play.api.templates.Html 
+3
scala template-engine playframework
source share
1 answer

You can use a workaround:

An example of a call in a template file:

 @TabsBuilder{ <a>tab1</a> }{ <a>tab2</a> }.map(tabs.apply) 

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

TabsBuilder allows you to write code as if you had a variable number of parameter lists.

+1
source share

All Articles