nightograph
nightograph

Reputation: 2249

how to dynamically load a scala template

I Wonder if there is any anyway of loading a template dynamically using playframework2 and scala using reflection. In play 1.x i used to do something like following:

   public static void template(String templateName) {
    renderTemplate("templateName");
}   

Is there anyway i can achieve the same thing using play2.x?

Java example/solution would be appreciated

Upvotes: 1

Views: 407

Answers (1)

Kim Stebel
Kim Stebel

Reputation: 42047

You can get the template object via reflection and then call its apply method using structural types.

val c = Class.forName(fullyQualifiedTemplateClassName + "$")
val template = c.getField("MODULE$").get(c).asInstanceOf[{def apply():Html}]

Ok(template())

Upvotes: 2

Related Questions