0
votes

I'm new to play framework and I'm trying to separate my app into multiple, smaller modules. For instance, I want to have a header module and a sidebar module.

For now, I've created these modules and a page module, that will render each module into its right place.

Here's an example of a module code:

package controllers

import play.api.templates._

object SidebarService {

  def getHTML() : Html = {
    views.html.sidebar(name = "variable", repeat = 5)
  }
}

Note that it is returning an Html object.

And here's an example of a page module

package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {

  def index = Action {
    var sideBar = SidebarService.getHTML()
    Ok(views.html.index(sideBar)("Your new application is ready."))
  }

}

This works fine, but I'm a little bit confused on how to get module-specific Assets to be included in page module, e.g. sidebar module has specific css and javascript files that will be "included" (and minified) in the <head> tag of page module. First, am I going in the right direction for modularization? Secondly, how could I accomplish module-specific assets?

Any pointers would be great.

1

1 Answers

1
votes

Regarding inclusion of module-specific assets, I guess you can always do something like:

package controllers

import play.api.templates._

object SidebarService {

  def getAssets : Html = {
    // your <link> and <script> tags here
  }

  def getBody : Html = {
    views.html.sidebar(name = "variable", repeat = 5)
  }
}

I suppose you're going to make something like PageService a trait with getAssets and getBody as abstract methods. Your approach for modularization seems OK. In order for the page to have an arbitrary number of modules, I guess I would pass as argument to views.html.index not only sideBar, but maybe a Map[String, PageService] containing the various modules identified by a string (or symbol, or any other thing you want) key, e.g. Map("header" -> HeaderService, "sidebar" -> SidebarService). That way, in the view, you can get all the assets for the modules you are using and lookup by the keys you defined to know what to draw in each placeholder.