I am using Play and I have a simple EmailService class that renders a few objects in a Play template. Here is my code:
Content html = views.html.acceptedEmail.render(incomingBlob,relatedBlob,rule);
email = new Email().setFrom("[email protected]")
.setTo(rule.getSuccessEmailValues())
.setSubject(rule.getFailureNotificationSubject())
.setBodyHtml(html.toString());
I'm wondering if it's possible to pass in the acceptedEmail template as a variable value so that the template I'm applying could change dynamically based on what value I was passing into the EmailService (Java) class this code is coming from.
I'm fairly new to Scala templating so apologies if the question is a little out there or if more information is required.
Update
This is ultimately how I'd like to have this code work:
String template = object.getTemplateName();
Content html = views.html."template".render(incomingBlob,relatedBlob,rule);
Obviously without the quotes, but hopefully you catch my drift.
As far as I'm aware, this isn't (entirely) possible as Play! compiles all the templates to be available as static (Scala) objects under the views.html.*
package/namespace. You may be able to achieve what you want by using Java's Refection API but to be honest I don't think it would be worth the time and there may be unknown issues with the class object that Scala will eventually produce when its compiled.
I suspect that the number of templates that you will need to render is definitely going to be finite (as they have to be compiled and can't be added at runtime) so your safest bet is to just verbosely check your condition and then choose the appropriate template i.e.:
Content content;
if(template == "acceptedEmail")
content = views.html.acceptedEmail.render(incomingBlob,relatedBlob,rule)
else if (template == "somethingElse")
content = views.html.somethingElse.render(incomingBlob,relatedBlob,rule)
.....
or you could do it at the view level:
//controller
Content content = views.html.mainView.render(template,incomingBlob,relatedBlob,rule);
//view - mainView.scala.html
@(template: String, incomingBlob: ?,relatedBlob: ?,rule: ?))
@if(template == "acceptedEmail") {
@acceptedEmail(incomingBlob, relatedBlob, rule)
}
In any case, this is not a trivial thing to achieve with play's templates so you may have better luck using another template library since this is just for emails (Perhaps Handlebars Java?).