我终于这样做了:
我创建了一个包views.helpers.form,其中包含:
bootstrap.scala.html:
@(elements: helper.FieldElements)
<div class="control-group@if(elements.hasErrors) { error}">
<label class="control-label" for="@elements.id">@elements.label(elements.lang)</label>
<div class="controls">
@elements.input
@elements.infos(elements.lang).map { info =>
<span class="help-inline">@info</span>
}
@elements.errors(elements.lang).map { error =>
<span class="help-block">@error</span>
}
</div>
复选框.scala.html:
@**
* Generate an HTML input checkbox.
*
* Example:
* {{{
* @checkbox(field = myForm("done"))
* }}}
*
* @param field The form field.
* @param args Set of extra HTML attributes ('''id''' and '''label''' are 2 special arguments).
* @param handler The field constructor.
*@
@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: helper.FieldConstructor, lang: play.api.i18n.Lang)
@boxValue = @{ args.toMap.get('value).getOrElse("true") }
@helper.input(field, args:_*) { (id, name, value, htmlArgs) =>
<label class="checkbox">
<input type="checkbox" id="@id" name="@name" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value))>
@args.toMap.get('_text)
</label>
div>
</div>
在我的模板中,我所要做的就是:
@import helper.{FieldConstructor, inputText, inputPassword} @** Import the original helpers *@
@import helpers.form.checkbox @** Import my helpers *@
@implicitField = @{ FieldConstructor(helpers.form.bootstrap.f) }
瞧!有用!