在Play 2(Scala)中创建自定义字段构造函数 [英] Creating a custom field constructor in Play 2 (scala)

查看:98
本文介绍了在Play 2(Scala)中创建自定义字段构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Play 2 Scala教程中给出的说明.我被困在编写自己的字段构造器"一节中.它提供了一个示例模板(无需说明文件名应为):

I am trying to grok the instructions given in the play 2 scala tutorial for form template helpers. I am getting stuck in the section "Writing your own field constructors". It gives a sample template (without saying what the name of the file should be):

@(elements: helper.FieldElements)

<div class="@if(elements.hasErrors) {error}">
    <label for="@elements.id">@elements.label</label>
    <div class="input">
        @elements.input
        <span class="errors">@elements.errors.mkString(", ")</span>
        <span class="help">@elements.infos.mkString(", ")</span> 
    </div>
</div>

然后显示此代码:

object MyHelpers {
  implicit val myFields = FieldConstructor(myFieldConstructorTemplate.f)    
}

我对这与模板之间的关系感到困惑. (例如,该模板文件应该被称为myFieldConstructorTemplate.scala.html吗?)我对此进行了一些尝试,但是没有运气.

I am confused about how this is supposed to relate to the template. (eg, is the template file supposed to be called myFieldConstructorTemplate.scala.html?) I tried some variations on this without luck.

我对scala和Play都是陌生的,但是我也知道play 2及其文档是新的,所以我不确定我缺少什么非常明显的东西.

I'm new to both scala and Play, but I also know play 2 and its docs are new, so I'm not sure what incredibly obvious thing I'm missing.

谢谢!

推荐答案

在本教程中,这段代码与文本相同:

In the tutorial this code is along the text:

通常,您将需要编写自己的字段构造函数.开始于 编写类似这样的模板:

Often you will need to write your own field constructor. Start by writing a template like:

这意味着您必须创建自己的模板(xxxx.scala.html)并在其中添加该代码.之后,使用它们提供的代码将其导入模板(请记住将其添加到使用自定义字段的每个scala模板中):

This means you have to create your own template (xxxx.scala.html) and add that code inside. After that, you import it in your template with the code they provide (remember to add that to each scala template that uses your custom field):

@implicitField = @{ FieldConstructor(xxxx.f) }

检查Play样本,Forms样本使用类似的方法进行Twitter引导.

Check the samples of Play, the Forms sample uses a similar approach with Twitter bootstrap.

这篇关于在Play 2(Scala)中创建自定义字段构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆