using twirl as runtime template engine

946 views
Skip to first unread message

Niels Boldt

unread,
Jan 20, 2015, 10:03:17 AM1/20/15
to play-fr...@googlegroups.com
Hi,

I'm currently building an application where I need to return html based on a html template and some input. But the challenge is that both the template html and input are dynamic so I cannot use twirl in the default way where templates are compiled into scala code together with the rest of the application.

So instead I was considering an approach where I somwhow used twirl to compile the templates on the fly to generate scala code and then also evaluate this scala code on the fly to create the final output.

Is that approach sensible and doable. I could easily hack something together using standard string substitution but I would really appreciate the power and flexibility that full blown use of twirl would give me

Best Regards
Niels




--
BinaryConstructors ApS
Vestergade 10a, 4th
1456 Kbh K
Denmark
phone: +4529722259
web: http://www.binaryconstructors.dk
mail: n...@binaryconstructors.dk
skype: nielsboldt

Aurélien

unread,
Jan 25, 2015, 1:48:11 PM1/25/15
to play-fr...@googlegroups.com
Hi,

It is possible to use Twril in a dynamic way, but it is not easy.
You can invoke Twril compiler with : TwirlCompiler.compile(template, templateDirectory, outputDirectory, formatter, imports, Codec.UTF8(), false, false);
It will generate a Scala file. Then you need to compile and execute this Scala file : http://stackoverflow.com/questions/7477589/compile-and-execute-scala-code-at-runtime
Eventually you will need to use reflection to use your compiled template.

However, if you really need dynamic templating, I advise you to use a template engine designed for dynamic use, for example :
- freemarker : http://freemarker.org/
- japid : https://github.com/branaway/Japid

Cheers,
Aurélien

James Roper

unread,
Jan 26, 2015, 9:38:08 PM1/26/15
to play-framework
Generating scala code and "evaluating" it (by that, you mean compiling then executing) is certainly possible, but not very sensible.  The scala compiler is very slow - just look at a scala projects build times compared to a java app.  That said, if you're able to cache the compilation, then it's not so bad.  I'm not sure of your particular use case, but a similar use case would be that the layout for a site is a scala template stored in a database.  If, when the app starts up, you compile the template and keep that reference to the compiled template, and then whenever the template is updated you recompile as well as save it to the database, then that should be fine, as long as you're not updating the template every few seconds.

So if you want an example of this, the best place to look is the Twirl tests:


Note that you'll need to write the template out to a temporary file, as well as needing working directories to generate the compiled template to and for the scala compiler to write its class files out to.

--
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/d/optout.



--
James Roper
Software Engineer

Typesafe – Build reactive apps!
Twitter: @jroper

ibsdevs

unread,
Jan 27, 2015, 6:00:42 AM1/27/15
to play-fr...@googlegroups.com
I need a solution for editable templates. My use case is following: Our app comes with default templates. The admin can edit a template and save it. 

Is runtime compilation of twirl templates the way to go for this use case? Or should I use something like Mustache? 

What happens if the admin creates a template with syntax errors? Is it possible to handle such errors while editing?

Thanks.

Aurélien

unread,
Jan 27, 2015, 6:25:24 AM1/27/15
to play-fr...@googlegroups.com
You may use Twirl if your admins cannot change the template parameters : this way you will avoid using reflection to use your template.
Else I advise you to use another template engine.

For the compilation errors, it seems like it is possible, check the Twril tests in the link James provided : https://github.com/playframework/twirl/blob/master/compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala
Reply all
Reply to author
Forward
0 new messages