Hi Ngoc,
Scalate is not really my cup of tea. So I have been invesigating other options. I found that
Jsoup would fit nicely with my needs. However Jsoup or XSLT for that matter, works on plain (XML,HTML,XML) files which is not that easy to implenent in Xitrum template engine.
Do you have a smart way to implement template logic in the Action and keep only the loading and caching of file within the template class.
Here is a mockup of my idea.
object JsoupEngine {
val TRANSFORMER: String = "JsoupTransformer"
}
class JsoupEngine extends TemplateEngine {
def renderView(location: Class[_ <: Action], currentAction: Action, options: Map[String, Any]): String = {
val doc = "src/main/html/" + location.getName.replace('.', File.separatorChar) + ".html"
val file = new File(doc)
val jdoc = Jsoup.parse(file, "UTF-8")
options.getOrElse(JsoupEngine.TRANSFORMER, currentAction) match {
case x: JsoupTransform => x.transform(jdoc).html()
case _ => file.toString
}
}
def renderFragment(location: Class[_ <: Action], fragment: String, currentAction: Action, options: Map[String, Any]): String = {
val doc = Jsoup.parseBodyFragment("")
this match {
case x: JsoupTransform => x.transform(doc).html()
}
}
}
object DefaultLayout extends JsoupTransform {
def transform(doc: Document): Document = {
doc.getElementById("body").text().concat("Jsoup transformation")
doc
}
}
trait DefaultLayout extends Action {
override def layout = renderViewNoLayout(classOf[DefaultLayout], Map(JsoupEngine.TRANSFORMER -> DefaultLayout))
override def respondView(customLayout: () => Any, location: Class[_ <: Action], options: Map[String, Any]): ChannelFuture = {
val opt = this match {
case x: JsoupTransform => options
case _ => options
}
super.respondView(customLayout, location, opt)
}
}
@GET("")
class SiteIndex extends DefaultLayout with JsoupTransform {
def execute() {
respondView()
}
override def transform(doc: Document): Document = {
doc.head().append("Do transformation of HTML file")
doc
}
}