Search code examples
grailsbuildgsp

Use a different layouts/main.gsp based on environment in Grails


I am trying to have two distinct layouts for my application. Most of the changes are CSS based, however some elements might also get moved around(such as moving a navbar from the top to the side). I decided the easiest would be is to have two main layouts, and pick one based on the environment that I compile for.

I am reading about the grails event scripts and how to hoop up on there. Ideally I would like to have red_main.gsp and blue_main.gsp and copy one of them to main.gsp during build time. Examples online on this are very limited and I would appreciate some insight.

Thank you!


Solution

  • Here is how I did for anyone stuck in the same boat. This is my scripts/_Events.gsp file:

    eventCompileStart = { msg -> 
       def envn = grails.util.Environment.current.name
       def mainName = "";
       if( envn.contains("_red" ) ) {
           mainName = "main_red.gsp"
       }
       else { 
           mainName = "main_blue.gsp"
       }
    
       ant.copy(file: "${basedir}/grails-app/views/layouts/${mainName}", 
               tofile: "${basedir}/grails-app/views/layouts/main.gsp", 
               overwrite: true) 
    }