2014-12-07 9 views

回答

1

事實上,這一想法在Custom Field Constructors documentation在這裏你可以找到相關的建築形式爲例更遠一部分的展開。

在Play框架中創建表單時,可以使用表單助手來簡化可重用輸入的創建。 Play包含幾個內置的助手,每個助手都將一個字段構造函數作爲一個隱式參數。

想象一下在視圖中創建的以下表單。

@implicitFieldConstructor = @{ helper.FieldConstructor(myFieldConstructorTemplate.f) } 

@helper.form(action = routes.Application.handlePost()) { 
    @helper.inputText(userForm("name")) 
    @helper.inputText(userForm("surname")) 
    @helper.inputText(userForm("login")) 
    @helper.inputText(userForm("password")) 
} 

的inputTextapply方法需要FieldConstructor作爲隱式參數。正如您所看到的,隱式模板用於爲apply方法提供一個值,而不會明確地將其傳遞給每個inputText調用。由於隱式參數,視圖中的表單定義看起來更簡潔明瞭。