如何在字段插件,grails中为相同的类使用自定义模板

时间:2014-07-04 13:17:29

标签: templates grails plugins field

例如,我有2个课程:

Person {
  String name
  String descriptionOfPerson
}

Company {
  String name
  String descriptionOfCompany
}

通常情况下,字段插件将为所有字段使用相同的模板/_fields/string/_field.gsp

但我想使用/_fields/string/_field.gsp的{​​{1}}模板和String name的{​​{1}}(以呈现ckeditor)/_fields/ckeditor/_field.gsp

有可能这样做吗?怎么样?

3 个答案:

答案 0 :(得分:1)

据我所知,该插件及documentation不可能,无需修改。

插件搜索模板的默认顺序是:

  1. 的grails-app /视图/ controllerName / actionName / propertyName的/
  2. 的grails-app /视图/ controllerName / actionName /属性类型/
  3. grails-app / views / controllerName / actionName /(此处不适用)
  4. grails-app / views / controllerName / propertyName /
  5. grails-app / views / controllerName / propertyType /(此处不适用)
  6. grails-app / views / controllerName /(此处不适用)
  7. 的grails-app /视图/ _fields /类/ propertyName的/
  8. 的grails-app /视图/ _fields /超类/ propertyName的/
  9. grails-app / views / _fields / associationType /(此处不适用)
  10. grails-app / views / _fields / propertyType /
  11. grails-app / views / _fields / propertySuperclass /(此处不适用)
  12. grails-app / views / _fields / default /
  13. 既没有可能传递自定义模板,也没有在通过属性类找到模板时区分属性名称。

    然而,有一些可能实现你的情况。

    A)模板条件

    您可以在/_fields/string/_field.gsp模板中添加条件并使用提供的模板参数,例如:

    <g:if test="${property == 'descriptionOfPerson' || property == 'descriptionOfCompany' }">
        <!-- render your ckeditor here or include another template via g:render -->
    </g:if>
    <g:else>
        <!-- render your normal input here or include another template via g:render -->
    </g:else>
    

    B)每个控制器属性的模板

    例如:
    grails-app/views/_fields/person/description/
    grails-app/views/_fields/company/description/
    两个模板都可以通过g:render包含另一个模板,位于grails-app/views/_fields/ckeditor/

    将此应用于您的示例,您可以将模板放置在任何粗体路径中,并且优先使用/_fields/string/_field.gsp模板。

    就我个人而言,我会坚持使用B),它允许更细粒度的控制,并且对于不熟悉您的代码的其他人更容易理解。我还要将两个字段重命名为description。与往常一样,这个决定取决于您的完整应用程序及其整体复杂性。

答案 1 :(得分:0)

字段插件的默认呈现机制(不使用覆盖模板内容)使用widget constraint of grails呈现textarea而不是<input type="text" />(如您所见in the source of the fields plugin)。< / p>

使用不引人注目的javascript库(ckeditor似乎具有dev guide中所述的这些功能),将<textarea>替换为更令人兴奋的内容,您唯一需要做的就是:

Person {
  String name
  String descriptionOfPerson

  static constraints = {
    descriptionOfPerson widget: 'textarea'
  }

}

Company {
  String name
  String descriptionOfCompany

  static constraints = {
    descriptionOfCompany widget: 'textarea'
  }
}

答案 2 :(得分:0)

至少现在(首发后2年),我找到了2个可行的解决方案:

(1)指定每个字段我想让wysiwyg编辑器功能成为视图中的子文件夹(如grails-app / views / myDomainObject / myWysiwygField / _wrapper.gsp),其中包含以下内容:

<div class="fieldcontain${required ? ' required' : ''}">
<label for="${property}">${label}<g:if test="${required}"> <span class="required-indicator">*</span></g:if></label>
<ckeditor:config var="toolbar_Mytoolbar">
[
    [ 'Undo', 'Redo', '-', 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList']
]
</ckeditor:config>
<ckeditor:editor name="${property}" width="40%" toolbar="Mytoolbar">${value}</ckeditor:editor> <!-- toolbar="Basic" -->
</div>

(2)将wysiwyg字段声明为textarea

static constraints = {
    myWysiwygField widget: "textarea"
}

并定义(一次)grails-app / views / _fields / textarea / _wrapper.gsp,其内容与上述内容相同。

所以第一个更精细,而第二个更容易解决更简单的问题。