4

I have this Scala/Play application and I have to fetch a bunch of templates via AJAX. I'm doing something like this now:

def home = Action {
    Ok(views.html.home())
}

def about = Action {
    Ok(views.html.about())
}

def contact = Action {
    Ok(views.html.contact())
}

//etc

But this is just creating an action for every template. Can I do something like this instead:

def loadTemplate(templateName) = Action {
    //Load template from "views" with name being value of parameter templateName
}

Is this possible on Play Framework? If so then how?

Play Framework 2.2.1 / Scala 2.10.3 / Java 8 64bit

UPDATE: My original question might have been misunderstood. I don't want to compile a template, I want to fetch already compiled one in a more dynamic way.

UPDATE2: I think I found something very close, if not exactly what I need on this answer, but it's in Java and I need it in Scala.

2
  • 1
    Hm, 'I want to fetch already compiled one...' is that mean that your views doesn't compile anything and they are just ready-to-use HTML pages ?
    – biesior
    Nov 29, 2013 at 20:39
  • @biesior the link in my second update pretty much covers what I need, it's just that I need it in Scala, not Java.
    – Caballero
    Nov 29, 2013 at 21:30

5 Answers 5

4

Using scala reflection:

object Application extends Controller {

  import reflect.runtime.universe._
  val currentMirror = runtimeMirror(Play.current.classloader)
  val packageName = "views.html."

  def index(name: String) = Action {
    val templateName = packageName + name

    val moduleMirror = currentMirror.reflectModule(currentMirror.staticModule(templateName))
    val methodSymbol = moduleMirror.symbol.typeSignature.declaration(newTermName("apply")).asMethod

    val instanceMirror = currentMirror.reflect(moduleMirror.instance)    
    val methodMirror = instanceMirror.reflectMethod(methodSymbol)

    Ok(methodMirror.apply().asInstanceOf[Html])
 }

 }
4
  • I'm trying to use this, but I'm getting an error: Execution exception[[IllegalArgumentException: wrong number of arguments]]
    – Caballero
    Nov 29, 2013 at 20:05
  • This code assumes your templates does take any parameters. If it does then you need to pass them to the apply method
    – Nilanjan
    Nov 30, 2013 at 15:11
  • So I suppose this method would only work if all the templates didn't have parameters, or had the same parameters? How would you pass parameters in your example?
    – Caballero
    Dec 1, 2013 at 12:07
  • I guess I was not clear. Templates get compiled to function objects. So if your template takes parameter you need to pass that to apply method. Ok(methodMirror.apply(<your parameter>).asInstanceOf[Html])
    – Nilanjan
    Dec 3, 2013 at 2:42
3

Based on @Nilanjan answer and for play-2.4 scala-2.11.7

  def page(page: String) = Action.async { implicit request =>
    Future.successful(Ok(loadTemplate(page)))
  }

  import reflect.runtime.universe._
  import play.api._
  import play.twirl.api.Html
  val currentMirror = runtimeMirror(Play.current.classloader)
  val packageName = "views.html."
  private def loadTemplate(name: String) = {
    val templateName = packageName + name
    val moduleMirror = currentMirror.reflectModule(currentMirror.staticModule(templateName))
    val methodSymbol = moduleMirror.symbol.info.member(TermName("apply")).asMethod
    val instanceMirror = currentMirror.reflect(moduleMirror.instance)
    val methodMirror = instanceMirror.reflectMethod(methodSymbol)
    methodMirror.apply().asInstanceOf[Html]
  }
1

Here we go again! @Nilanjan answer adopted to Scala 2.12 and Play 2.6 (as moving to DI play.api.Play.current, TermName and declaration have been deprecated). For app, I had to use injector because @Inject()(app: Application) was causing circular dependency

class HomeController @Inject()(injector: Injector, cc: ControllerComponents) extends AbstractController(cc) {
  def index(name: String = "index") = Action { implicit request: Request[AnyContent] =>
    import reflect.runtime.universe._

    val app = injector.instanceOf[Application]
    val currentMirror = runtimeMirror(app.classloader)
    val packageName = "views.html."

    val templateName = packageName + name

    val moduleMirror = currentMirror.reflectModule(currentMirror.staticModule(templateName))
    val methodSymbol = moduleMirror.symbol.typeSignature.decl(TermName("apply")).asMethod

    val instanceMirror = currentMirror.reflect(moduleMirror.instance)
    val methodMirror = instanceMirror.reflectMethod(methodSymbol)

    Ok(methodMirror.apply("some", "content").asInstanceOf[Html])
  }
}
0

It's not quite good idea to allow search view by any text (for security reasons) as such can be passed in param, instead, you can resolve this quite easy with match statement - it will allow you restrict request to allowed views only and will handle wrong requests as well, probably Scala geeks can demonstrate nicer code, but this will work for you out of the box:

def loadTemplate(templateName: String) = Action {

  templateName match {
    case "about" => Ok(about())
    case "home" => Ok(home())
    case "contact" => Ok(contact())
    case _ => NotFound(notFoundView())
  }

}

route:

GET  /load-template/:templateName   controllers.Application.loadTemplate(templateName)

Additional benefit is that you can grab additional data from request and pass it to the resolved view depending on templateName param

4
  • Thanks, I realize I could do this, however I was hoping for a more dynamic solution - the one that doesn't require a zillion case statements (in my situation).
    – Caballero
    Nov 28, 2013 at 21:28
  • You can't add new view without re-deploy, can you? Even if you'll add view zillion+1 you will need to restart the prod app, in such case adding next line into loadTemplate action should not be a problem. On the other hand, if you are going to use all of the views without the params (as just static HTML pages) why don't you consider storing them in database? In such case you can create micro CMS for managing zillion^10 so you will be able add/edit/delete pages on the flight.
    – biesior
    Nov 28, 2013 at 21:35
  • 1
    I think we have a misunderstanding here. I don't want to compile any templates - they're all already compiled at the time of application launch. All I want is a more dynamic way to fetch them - without hardcoding all those case statements, that's all.
    – Caballero
    Nov 29, 2013 at 20:08
  • This solution is not sustainable having to edit and recompile sources every time a new page case is needed, more so when I'd like to segregate the views depending on the language.
    – SkyWalker
    Oct 5, 2019 at 11:17
0

In your Template file

general_template.scala.html

@(templateName : String = "none")

@templateName match {       

 case "about" => { //html elements}

 case "home" => {//html elements}

 case "contact" => {//html elements}

 case _ => { }

}

and in your controller

def loadTemplate(templateName) = Action {
    Ok(views.html.general_template(templateName))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.