Play 2.1 Scala: Render template from string

139 views
Skip to first unread message

Eneko Alonso

unread,
Feb 19, 2013, 6:25:10 PM2/19/13
to play-fr...@googlegroups.com
How can I render a template from a string? For example, if I load a template from a database table, how can I compile it on the fly and render it?

Thanks,
Eneko

Bing Ran

unread,
Feb 20, 2013, 10:16:08 PM2/20/13
to play-fr...@googlegroups.com
Probably hard if not impossible with the Scala templates.  

In case you would consider alternate template engine, I added the support to Japid42 (https://github.com/branaway/japid42) yesterday, so one can do something like the following in a controller:

public static Result dynamic() {
String template = "@(String s, int i)\nHi ${s}, are you ${i} years old?";
return ok(renderDynamic(template, "Johnny", 13));
}

The dynamic inline script is compiled to jvm bytecode and cached. It can use whatever other templates (tags, etc) on your harddrive.  

Remember though, the dynamic scripts must be managed in your hands, or you risk exposing the runtime system to attackers. 

Bing



2013/2/20 Eneko Alonso <eneko....@gmail.com>
How can I render a template from a string? For example, if I load a template from a database table, how can I compile it on the fly and render it?

Thanks,
Eneko

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nilanjan Raychaudhuri

unread,
Feb 25, 2013, 3:39:49 PM2/25/13
to play-fr...@googlegroups.com
Do you really want to compile the templates on the fly? How are these templates generated?

Well one possible way would be to compile the template to scala using the ScalaTemplateCompiler.compile. But I would like to understand the usecase better

Nilanjan, Developer & Consultant
Typesafe Inc.
Twitter: @nraychaudhuri
Reply all
Reply to author
Forward
0 new messages