Question

In our application, numerous emails are being sent from the system. These emails were of the same format for all users with different contextual variables populating the dynamic data.

We are now planning a feature to allow administrators to edit and customize these templates. As such the plan is to use the groovy shell to evaluate the templates at run time e.g.

Binding binding = new Binding();
binding.setVariable("model", [var1: "First Name", var2: "Last Name"])
GroovyShell shell = new GroovyShell(binding);

Object email = shell.evaluate('return "<html><title>Test Shell</title><body>${model.var1} ${model.var2}</body></html>";');

This seems to work adequately for us. The questions I have are:

  1. Is the GroovyShell the preferred engine to use or is Rhino or other better?
  2. Are there any performance concerns or memory issues to be aware of? Any low hanging fruit we can optimize i.e. can the shell or binding be reused
  3. What's the biggest bottleneck in the above code? The construction? The evaluation?

thanks

Was it helpful?

Solution

I would recommend using something like GroovyPagesTemplateEngine because it goes beyond just Groovy eval and you and you can use all the grails taglib goodness as well. I'm using both GroovyPagesTemplateEngine and SimpleTemplateEngine for your exact scenario.

SimpleTemplateEngine is slightly faster so if I don't need much more than simple binding I use it. When I need to deal with logic and control structures, I use GroovyPagesTemplateEngine.

OTHER TIPS

For grails, use the page rendering api instead. http://grails.org/doc/2.0.x/guide/introduction.html#whatsNew

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top