How to call a template which accepts variable number of args in Play Framework 2

前端 未结 3 841
悲&欢浪女
悲&欢浪女 2021-01-12 08:54

The Play Framework 2 template language is pretty nice. However, though it’s ‘inspired by’ Microsoft’s Razor language, one important design decision is different: how you ‘es

3条回答
  •  难免孤独
    2021-01-12 09:30

    Here's what you might be looking for. It's not exactly FP though

    structuredpage.scala.html

    @(title: String)(content: scala.collection.mutable.MutableList[Pair[String, Html]] => Unit)
    
    @main(title){
        @defining(new scala.collection.mutable.MutableList[Pair[String,Html]]()) { sections =>
            @content(sections)
            @for(section <- sections){
                

    @section._1

    @section._2
    } } }

    frontpage.scala.html

    @()
    
    @import views.Common.section
    
    @structuredpage("Front Page") { implicit sections =>
        @section("Section 1") {
            

    stuff

    } @section("Section 2") {

    more stuff

    } }

    section method:

    def section(title: String)(content: Html)(implicit sections: scala.collection.mutable.MutableList[Pair[String, Html]]) {
        sections += title -> content
    }
    

提交回复
热议问题