Thursday, January 18, 2007

Grails Templating

Since Grails 0.4 (not yet released, but downloadable as snapshot), you can customize how Grails creates/generates artifacts and scaffoldig.

The creation of artifacts like domain classes, controllers, services etc., but also the generation of the scaffolding controller and views, are now template based. As Grails developer you will notice no difference in contrast to previous version, as the the Grails distribution has default templates embedded.

However, for customizing your project needs, you can now take the advantage of customizing these templates for each application. These customization may vary from just extending a BaseController to completely customized layout in the views. Especially customizing the generated HTML in the views offers a lot of potential for developing your applications faster. Changing the same HTML code in generated views is time consuming, but with the customized templates you just do it once, and get it anywhere!

To customize the templates you will need to install the templates. Go into your project's basedir and type:
grails install-templates

This will install the templates in the src/templates folder of your project. Two kind of templates will be installed: artifact and scaffolding templates. The artifact templates are templates which will be used when creating artifacts with the create-xxx command line tools. The scaffolding templates will be used for scaffolding. There are scaffolding templates for the controller and for each view (list.gsp, create.gsp, edit.gsp, show.gsp).

After installing the templates, Grails will use these templates instead of the embedded default templates. If you later don't want to use customized templates anymore, just delete the customized templates from your project and the embedded default templates will be used again.

See also

No comments: