hakuna1811
hakuna1811

Reputation: 534

how to use custom templates for same classes in field plugin, grails

For example I have 2 class:

Person {
  String name
  String descriptionOfPerson
}

Company {
  String name
  String descriptionOfCompany
}

Normally, fields plugin will use the same template /_fields/string/_field.gsp for all of them.

But I want to use /_fields/string/_field.gsp template for String name and /_fields/ckeditor/_field.gsp (to render ckeditor) for descriptionOfPerson and descriptionOfCompany

Is it possible to do like that? And how ?

Upvotes: 0

Views: 1247

Answers (3)

Eicke Godehardt
Eicke Godehardt

Reputation: 51

At least now (2 years after initial post) I found 2 working solutions for me:

(1) specify for every field I want to have wysiwyg editor functionality a sub-folder in views (like grails-app/views/myDomainObject/myWysiwygField/_wrapper.gsp) with the following content:

<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) declare the wysiwyg fields as textarea

static constraints = {
    myWysiwygField widget: "textarea"
}

and define (once) grails-app/views/_fields/textarea/_wrapper.gsp with the same content than above.

So the first one is more fine-granular while the second one is more appealing for simpler problems.

Upvotes: 0

Mario David
Mario David

Reputation: 1615

The default rendering mechanism of the fields plugin (without using the override template stuff) uses the widget constraint of grails to render a textarea instead of a <input type="text" /> (as you see in the source of the fields plugin).

With an unobtrusive javascript library (ckeditor seems to have these capabilities as described in their dev guide), that replaces a <textarea> with something more exciting, the only thing you have to do is the following:

Person {
  String name
  String descriptionOfPerson

  static constraints = {
    descriptionOfPerson widget: 'textarea'
  }

}

Company {
  String name
  String descriptionOfCompany

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

Upvotes: 0

croeck
croeck

Reputation: 766

As far as I know the plugin and it's documentation this is not possible without modifications on your side.

The default order in which the plugin searches for the templates is:

  1. grails-app/views/controllerName/actionName/propertyName/
  2. grails-app/views/controllerName/actionName/propertyType/
  3. grails-app/views/controllerName/actionName/ (not applicable here)
  4. grails-app/views/controllerName/propertyName/
  5. grails-app/views/controllerName/propertyType/ (not applicable here)
  6. grails-app/views/controllerName/ (not applicable here)
  7. grails-app/views/_fields/class/propertyName/
  8. grails-app/views/_fields/superclass/propertyName/
  9. grails-app/views/_fields/associationType/ (not applicable here)
  10. grails-app/views/_fields/propertyType/
  11. grails-app/views/_fields/propertySuperclass/ (not applicable here)
  12. grails-app/views/_fields/default/

Neither is there a possibility to pass a custom template, nor to distinguish between between the property name when the template is found via the property's class.

However, there are some possibilities to realize your case.

A) Template condition

You could include a condition into the /_fields/string/_field.gsp template and make use of the provided template parameters, such as:

<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) Template for each controller property

for instance:
grails-app/views/_fields/person/description/ and
grails-app/views/_fields/company/description/.
Both templates could include another template via g:render, being place at grails-app/views/_fields/ckeditor/

Applying this to your example, you could place the template in any of the bold paths and it would be preferred to the /_fields/string/_field.gsp template.

Personally I would stick with B), which allows a more fine-grained control and is easier to understand for others that are not familiar with your code. I would also rename both of your fields to description. As always, this decision depends on your complete application and its overall complexity.

Upvotes: 1

Related Questions