例如我有2类:
Person {
String name
String descriptionOfPerson
}
Company {
String name
String descriptionOfCompany
}
通常,fields插件将对所有模板使用相同的模板
/_fields/string/_field.gsp
。但是我想对
/_fields/string/_field.gsp
和String name
使用/_fields/ckeditor/_field.gsp
模板(以渲染ckeditor)对descriptionOfPerson
和descriptionOfCompany
有可能这样做吗?如何 ?
最佳答案
据我所知,这个插件是documentation,如果没有您的修改,这个是不可能的。
插件搜索模板的默认顺序为:
通过属性的类找到模板时,既不能传递自定义模板,也不能在属性名称之间进行区分。
但是,有一些实现您的情况的可能性。
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)每个 Controller 属性的模板
例如:
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
。与以往一样,此决定取决于您的完整应用程序及其整体复杂性。
关于templates - 如何在字段插件中对相同的类使用自定义模板,grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24575266/