使用Html类型的可变参数调用play2模板 [英] Call play2 template with variable arguments of type Html
本文介绍了使用Html类型的可变参数调用play2模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何调用带有可变类型的参数的模板,这些参数具有Html类型?
How can I call a template with a variable number of arguments that have Html type in play?
我在play2中创建了一个模板,定义如下:
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>
}
我试图这样称呼
@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
推荐答案
您可以使用解决方法:
模板文件中的示例调用:
Example call in a template file:
@TabsBuilder{
<a>tab1</a>
}{
<a>tab2</a>
}.map(tabs.apply)
TabsBuilder:
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))
}
通过TabsBuilder,您可以像编写可变数量的参数列表一样编写代码.
The TabsBuilder enables you to write the code like you would have a variable number of parameter lists.
这篇关于使用Html类型的可变参数调用play2模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文