Dynamic Templates in Play Framework 2.0

A Play 1.0exists TemplateLoaderfor generating patterns at runtime.

Is there any solution for dynamically loading a template in Play 2.0? Or can I somehow convert it to scala code for use Eval?

For example: I want to save some templates in a database so that some users can edit them.

+3
source share
2 answers

It seems that the corresponding code is in the framework/src/play/src/main/scala/system/ApplicationProvider.scalaPlay 2.0 directory, especially in the classroom ReloadableApplication. I'm not sure how this compiler will work on the fly for you, because you do not want to do this when a template is requested (it is slow). This means that storing in the database does not make much sense: you do not want to store the source code of the template, but rather a compiled template object.

For arguments, if you just wrote templates in a directory app/views, you can leave Play to compile them at your leisure. But then be careful, because they probably won’t compile in the production system.

+2
source

Play 2.0 already compiles your templates to object methods, so you don’t need to dynamically load them.

app/views/test.scala.html.

@(num:Long) 
Your number is @num

Scala views.html, test. :

val msg : String = views.html.test(23).toString()

html-. , play.api.templates.Txt . app/views/quick.scala.txt:

@(id:Long)Your id is @id

views.txt.quick :

val msg2 : String = views.txt.quick(32).body

play.api.templates.

+3

All Articles