2012-05-16 88 views
6

我想學習與斯卡拉播放2.0,但我不認爲我很理解如何2.0模板系統的作品。我之前使用過Play 1.2,我正在尋找與#{include'views/blah.html'/}等效的內容。我基本上想要創建一個在所有頁面上呈現的導航欄。包括scala.html文件在播放2.0斯卡拉

基本上main.scala.html我有

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

<!DOCTYPE html> 

<html> 
    <head> 
    <title>@title</title> 
    <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"> 
    <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")"> 
    <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script> 
    </head> 
    <header> 
    This is my header 
    </header> 
    <section class="navbar">@navbar</section> 
    <section class="content">@content</section> 
    <footer> 
    This is my footer 
    </footer> 

,並在我的index.scala.html:

@navbar = { 
<h1>Index</h1> 
<ul> 
    <li> 
     <a [email protected]>Tasks</a> 
    </li> 
</ul> 
} 
@main("Home")(navbar){ 
    content 
} 
在task.scala.html

@(tasks: List[Task], taskForm: Form[String]) 
@import helper._ 
@main("Home") { 
<h1>Index</h1> 
<ul> 
    <li> 
     <a [email protected]>Tasks</a> 
    </li> 
</ul> 
} { 
    task code 
} 

現在包括這個na VBB似乎我必須在每一頁這樣重複這一點,我將不得不將這個導航欄硬編碼到每一頁。有沒有辦法做到這一點,而無需在每個頁面中編寫整個導航欄?

我也試圖創建一個包含

<h1>Index</h1> 
<ul> 
    <li> 
     <a [email protected]>Tasks</a> 
    </li> 
</ul> 

一個navbar.scala.html文件,並根據意見節省/然後導入,使用@import views.navbar但後來我得到一個錯誤,說明「導航欄不是意見的成員」。如果有幫助,我在Eclipse Java EE IDE indigo中編寫它。

回答

7

不要進口,但只是把它:

@navbar() 
+0

:BaseScalaTemplate([email protected]),而不是實際的navbar.scala.html – Darbs

+0

你能告訴我們你的代碼? – Somatik

+0

哦,是的,你可能需要添加parens。我編輯了我的答案。 –

0

包括任何其他視圖模板到另一種意見模板, 你簡單的把它用:@views.html.[location].[location].[location]()

其中[位置]只是休息它的路徑。

例如:

@views.html.users.interface() 

務必把「()」,即括號在聲明的結尾,如果它不帶任何參數。如果沒有「()」,您將收到如下錯誤消息: 「BaseScalaTemplate(play.api.templates ...)」

如果您的模板有參數,請確保在調用它時包含它們這樣的:

@views.html.users.interface("name") 
當我使用「@navbar」它會顯示這個