2014-12-23 51 views
2

我一直在閱讀Play上的文檔!網站,並且看不到它解釋了什麼參數組以及它們將用於什麼。玩!框架模板參數組

之間有什麼區別:

@(title: String)(user: User) 

和:

@(title: String, user: User) 

如果有人可以幫助我在這裏會不勝感激。

+1

請看此 http://stackoverflow.com/questions/4684185/why-does-scala-provide-both-multiple-parameters-lists-and-multiple-parameters-pe – mgosk

回答

0

正如@mgosk在評論中所暗示的,多個參數列表是Scala函數的一個標準特性,Google可以回答得比我更好。

關於特別播放模板但是,它們非常有用。

TLDR在播放模板參數組是有用的:

  • 傳遞隱式參數到模板(從動作和父模板)
  • 提供一個很好的方式經由Scala的塊調用語法來包裝內容您可以使用它們

一個原因是形式傳遞隱含參數的觀點。隱參數是由編譯器添加到呼叫現場,並作爲spec points out他們必須在最後一個(或唯一的)參數列表中標明,例如:

@(title: String)(implicit request: RequestHeader) 

<h1>@title</h1> 

The path is for this request is <b>@request.path</b>. 

當調用從一個動作你不認爲模板只要有一個請求頭(see here for more details on that),就不必明確提供請求頭。

模板中多參數列表的另一個非常有用的用途是「包裝」內容,尤其是考慮到可以用braces instead of parenthesis調用Scala函數。假設您有一些代表多個不同小部件的部分模板,但這些模板總是被相同的樣板HTML包圍。然後,您可以創建一個模板包裝(稱爲widgetWrapper.scala.html)是這樣的:

@(name: String)(details: Html) 

<div class="item"> 
    <div class="item-name">@name</div> 
    <div class="item-body"> 
    @details 
    </div> 
</div> 

這可以被調用,像這樣:

@(item: WidgetA) 

@widgetWrapper(item.name) { 
    <ul class="item-details"> 
    <li>@item.detail1</li> 
    <li>@item.detail2</li> 
    </ul> 
} 

這最後的技術是如何定義標準化「頁鉻」或網站例如(文件standardLayout.scala.html):

@(title: String)(content: Html)(sidebar: Html) 

<html> 
    <head> 
    <title>@title</title> 
    </head> 
    <body> 
    <header> 
     <h1>@title</h1> 
    </header> 
    <article>@content</article> 
    <aside>@sidebar</aside> 
    </body> 
</html> 

使用像這樣:

@() 

@standardLayout("My Page on Goats") { 
    <p> 
    Lots of text about goats... 
    </p> 
} { 
    <ul class="sidebar-links"> 
    <li><a href="https://en.wikipedia.org/wiki/Goat">More about goats!</li> 
    </ul> 
} 

如您所見,兩個大括號分隔的部分中的內容分別作爲Twirl Html傳遞給佈局模板,分別作爲主內容和邊欄(將第一個參數中傳遞的標題作爲字符串傳遞。)

由多個參數組使得這種操作變得可能。