Search code examples
templatesgrailsgrails-plugingrails-3.0

How to create reusable layouts and scaffolding templates in Grails 3.x


I am fairly new to Grails and I am having a hard time finding information on this. I'm coming from CakePHP where you can easily create a plugin to theme you application and I am trying to replicate this kind of setup in Grails.

I want to create re-usable layouts and scaffolding templates so that all applications I create with Grails will have a common look and feel. I have successfully customized layouts and scaffolding templates in one Grails application and was exploring packaging this into a plugin that could be imported in another project but I have not managed to make this work yet (I did package the plugin and import it in another project but I can't seem to figure out how to access the plugin's layouts and scaffolding templates from the applications).

What is the best way to achieve this? Is it even a good idea to package this as a plugin or should I simply overwrite Grails' default main layout and import my own scaffolding template in every project?

If going the plugin route is a good way to go:

  1. How do I apply my plugin's layouts and scaffolding template from my application
  2. I would like to create a "help page" in the plugin that would be accessible from the main application (in dev only). This page would enable me to confirm that the layouts are working fine and would also provide other developers using this plugin instructions on how to use the plugin's templates. How to I access a web page from the plugin through my Grails application?

If the plugin is not the way to go, what do you recommend?


Solution

  • Yes, a plugin is a great way to go. I'll simply quote the documentation:

    When a plugin provides a controller it may also provide default views to be rendered. This is an excellent way to modularize your application through plugins. Grails' view resolution mechanism will first look for the view in the application it is installed into and if that fails will attempt to look for the view within the plugin. This means that you can override views provided by a plugin by creating corresponding GSPs in the application's grails-app/views directory.

    For example, consider a controller called BookController that's provided by an 'amazon' plugin. If the action being executed is list, Grails will first look for a view called grails-app/views/book/list.gsp then if that fails it will look for the same view relative to the plugin.

    Basically, you can access a plugin's views by accessing them as if the views are in the application itself.