我已經使用Play 2.0框架幾天了,以便在我的工作中獲得概念應用程序的證明。我想要檢查的第一件事是自定義標籤功能,因爲它讓我想起了ASP.Net MVC中的HtmlHelpers。事情是,我似乎無法讓他們工作,並想知道如果我濫用功能或誤解的東西。Java/Scala:理解Play框架自定義標籤或助手
下面是我想要做的一個簡單示例:我希望能夠在模板中的任意位置使用@script(「scriptname.js」),並使其替代整個標籤。
這是我走到這一步:
main.scala.html
@(title: String, scripts: Html = Html(""))(content: Html)
@import tags._
<!DOCTYPE html>
<html>
<head>
<!-- this is how I would like to use the helper/tag -->
@script("jquery.js")
@script("jquery-ui.js")
<!-- let views add their own scripts. this part is working OK -->
@scripts
</head>
<body>
@content
</body>
</html>
我創建了一個名爲 「標籤」 子目錄中的應用程序/視圖目錄下。在那裏,我創造了我script.scala.html標籤/輔助文件:
@(name: String)
<script src="@routes.Assets.at("javascripts/@name")" type="text/javascript"></script>
我遇到的問題是,每當我使用@Script()的輸出包括在它的@name參數。例如@script(「x.js」)實際輸出
<script src="assets/javascripts/@name" type="text/javascript"></script>
我在做什麼錯?
爲了記錄在案,我看過的文件和搜索結果,但無論這些鏈接幫助的我想出解決辦法:
http://www.playframework.org/documentation/2.0.3/JavaTemplateUseCases
How to define a tag with Play 2.0?
謝謝,這確實的伎倆來實現的。那麼我假設一旦我在表達式中,我不再需要在它們之前使用@引用變量或方法? – enriquein
準確地說,它只是Scala代碼。 – themel