Search code examples
crafter-cms

controller script running in studio or deployment


Is there a way in a groovy controller script to know is it's running in the studio or in deployment server?

I have code that will added data into the model for demo in studio mode while in deployment that data will come from a REST service call.


Solution

  • Yes there's an easy way to know if a Groovy script is running in authoring (preview) or in delivery (live). Crafter provides a global variable called modePreview. This variable is basically a boolean flag that indicates if the current environment is authoring, or in other words, if Crafter Engine is running in preview mode. So you can do something as simple as if (modePreview) { RUN CODE } in Groovy to run code that only needs to be run in the authoring environment.