2014-01-06 12 views
5

我還沒有在Ruby框架中找到類似於Ruby on Rails部分視圖的部分視圖的概念。例如,如果有layouts/main.scala.html佈局:Play中存在部分視圖?

@(title: String)(content: => Html)(implicit flash: Flash) 
<!DOCTYPE html> 
<html> 
    <head> 
     <title>@title</title> 

    </head> 
    <body> 
     <section class="content">@content</section> 
    </body> 
</html> 

而且還有layouts/_footer.scala.html「部分」,我怎麼包括_footermain? Play中有沒有類似的東西?

回答

16

我想回報率的部分觀點是過於複雜。關於Play模板需要記住的事情,因爲它們本質上只是可以從Scala代碼直接調用的函數。而且,Play模板本質上是Scala代碼。這意味着,可以從其他Play模板調用Play模板。所以,只要創建一個名爲footer.scala.html另一個模板,如:

<footer> 
    Powered by Play Framework 
</footer> 

,然後從主模板調用它,你將調用任何其他斯卡拉功能:

@(title: String)(content: => Html)(implicit flash: Flash) 
<!DOCTYPE html> 
<html> 
    <head> 
     <title>@title</title> 
    </head> 
    <body> 
     <section class="content">@content</section> 
     @footer() 
    </body> 
</html> 

再簡單不過了。

+0

如何玩知道@footer定義在哪裏? –

+5

@Alex在這種情況下,必須在'views.html'命名空間中定義'@ footer',它將爲Play模板自動導入。如果它位於'app/views/common/footer.scala.html'中,那麼它就是'common.footer()'。 –

0

我認爲@Vidya想要說的是,你可以做這樣的事情:

在main.scala.html我們添加HTML類型的頁腳變量命名爲空默認值:

@(title: String, footer: Html = Html(""))(content: Html) 

    <!DOCTYPE html> 

    <html> 
     <head> 
      <title>@title</title> 
     </head> 
     <body> 
      @content 
      @footer 
     </body> 
    </html> 

,然後在頁面像index.scala.html我們可以這樣做:

@(message: String) 

@footer = { 
    <footer>the footer!</footer> 
} 

@main("Welcome", footer) { 
    the content! 
}