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

前端 未结 3 817
悲&欢浪女
悲&欢浪女 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:11

    I can't test this right now in this machine, but the following should work (no need of auxiliar method):

    @()
    
    @structuredpage("Dashboard"){
       ("Latest Requests", {
            <p>Blah</p>
        }),
       ("Your Details", {
            <p>Blah blah</p>
        })
    }
    
    0 讨论(0)
  • 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 id="@section._1">
                    <h2>@section._1</h2>
                    @section._2
                </section>
            }
        }
    }
    

    frontpage.scala.html

    @()
    
    @import views.Common.section
    
    @structuredpage("Front Page") { implicit sections =>
        @section("Section 1") {
            <h1>stuff</h1>
        }
    
        @section("Section 2") {
            <h1>more stuff</h1>
        }
    }
    

    section method:

    def section(title: String)(content: Html)(implicit sections: scala.collection.mutable.MutableList[Pair[String, Html]]) {
        sections += title -> content
    }
    
    0 讨论(0)
  • 2021-01-12 09:30

    Here is a workaround:

    @import views.Common.section
    
    @sec1 = { <p>Blah</p> }
    
    @sec2 = { <p>Blah blah</p> }
    
    @structuredpage("Dashboard")(
        section("Latest Requests")(sec1),
        section("Your Details")(sec2)
    )
    

    previous attempt:

    I think your wish makes it to complicated. Templates should be simple. Here is a simple alternative:

    index.scala.html

    @structuredpage("Dashboard"){
        @section("Latest Requests") {
            <p>Blah</p>
        }
    
        @section("Your Details") {
            <p>Blah blah</p>
        }
    }
    

    section.scala.html

    @(title: String)(content: Html)
    
    <section id="@title">
        <h2>@title</h2>
        @content
    </section>
    

    structuredpage.scala.html

    @(title: String)(sections: Html)
    
    @main(title){
        <nav class="page-links">
            table-of-contents goes here
        </nav>
        @sections
    }
    

    I made a gist: https://gist.github.com/4280577 . So you can check it out and play with it.

    0 讨论(0)
提交回复
热议问题