是否有可能重写形式佣工?(Is it possible to override form helpe

2019-06-23 18:08发布

使用医生,我可以把我自己的助手为布局surrending我的领域,但我想还以个性化的发挥给予一定的领域。

主要原因是Twitter的引导2,在这里我需要改变(在checkbox.scala.html)

@input(field, args:_*) { (id, name, value, htmlArgs) =>
    <input type="checkbox" id="@id" name="@name" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value))>
    <span>@args.toMap.get('_text)</span>
}

至 :

<label class="checkbox">
    <input type="checkbox" name="@name" id="@id" value="@boxValue" @(if(value == Some(boxValue)) "checked" else "") @toHtmlArgs(htmlArgs.filterKeys(_ == 'value)) />
    @args.toMap.get('_text)
</label>

我怎样才能做到这一点 ? 谢谢你的帮助!

Answer 1:

我终于做到了这样的:

我创建了一个包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>

checkbox.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) }

瞧! 有用!



Answer 2:

这将是简单的只写你自己的标记你想要的代码,并使用它,而不是提供帮助的。 这将简化相关overwritting平台标签的潜在问题。



文章来源: Is it possible to override form helpers?