New template engine

78 views
Skip to first unread message

lsPeter

unread,
May 8, 2013, 8:01:56 AM5/8/13
to xitrum-f...@googlegroups.com
Hi Ngoc,

You have talked about replacing Scalate with another faster template engine.

Do you have an idea which template engine you prefere.
Or do you have a shortlist ?

I am not trying to start an debate about the "best" template engine, but I just want to know where Xitrum is going before I dig into version 2.x. 

Thanks
/Peter

Ngoc Dao

unread,
May 8, 2013, 10:28:52 AM5/8/13
to Xitrum web framework
You don't have to be worried about Scalate being replaced. Scalate
will not be replaced, I'm just thinking about adding several template
engines so that you can choose. Adding a new template engine is
somewhat trivial with the current architecture of Xitrum.

I've searched and found these:
https://github.com/lihaoyi/scalatags
http://stackoverflow.com/questions/5281089/are-there-any-scala-template-engines-other-than-scalate

If you know others, please propose.

lsPeter

unread,
May 24, 2013, 11:03:29 AM5/24/13
to xitrum-f...@googlegroups.com
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
  }
}

Ngoc Dao

unread,
May 24, 2013, 9:19:18 PM5/24/13
to Xitrum web framework
What do you want to do with jsoup?
Why don't you use XML feature in Scala?


On 5月25日, 午前12:03, lsPeter <pe...@easyspeedy.com> wrote:
> Hi Ngoc,
>
> Scalate is not really my cup of tea. So I have been invesigating other
> options. I found that Jsoup <http://jsoup.org/> would fit nicely with my

Oleksandr Bezhan

unread,
Oct 4, 2013, 4:45:42 AM10/4/13
to xitrum-f...@googlegroups.com
I really like Lift's approach. How about Lift's DSL for template processing ?

Середа, 8 травня 2013 р. 15:01:56 UTC+3 користувач lsPeter написав:

Ngoc Dao

unread,
Oct 4, 2013, 8:33:12 AM10/4/13
to xitrum-f...@googlegroups.com
Can you give examples about what you like in Lift's template?

Oleksandr Bezhan

unread,
Oct 7, 2013, 7:20:36 AM10/7/13
to xitrum-f...@googlegroups.com
Lift uses snippets concept. Snippet receives html and transforms it into another html. And html template is plain html - no scriplets.

I think we could pull it's implementation from https://github.com/lift/framework web-kit module and integrate it into Xitrum as an alternative to Scalate.


Середа, 8 травня 2013 р. 15:01:56 UTC+3 користувач lsPeter написав:
Hi Ngoc,

Ngoc Dao

unread,
Oct 8, 2013, 12:44:42 PM10/8/13
to xitrum-f...@googlegroups.com
I think it's hard to integrate that into Xitrum, because the philosophy of Lift is pull (starting from a template, you pull snippets into it), whereas the philosophy of Xitrum is the opposite, push (starting from an action, you push data to templates).

Also, I think Lift way is verbose. Actually one reason I created Xitrum is I hate Lift's verbosity. I really wanted to like Lift but I couldn't. In 2009 I tried Lift for several months, then I decided to create Xitrum because I couldn't stand Lift anymore.

I mean I don't want to implement the integration of Lift's template into Xitrum myself. If you like please go ahead, implement and send me a pull request. I will be happy to merge that into Xitrum.

Ngoc Dao

unread,
Oct 8, 2013, 1:25:20 PM10/8/13
to xitrum-f...@googlegroups.com
A note about implementing a new template engine for Xitrum:

A template engine for Xitrum should be structured as a separate project, not put in Xitrum core. See this guide:
http://ngocdaothanh.github.io/xitrum/guide/template_engines.html#create-your-own-template-engine

Oleksandr Bezhan

unread,
Oct 9, 2013, 2:54:51 AM10/9/13
to xitrum-f...@googlegroups.com
Could you please provide some examples where Lift seems to be verbose compared to Xitrum ?
It would be also great to have such examples published on the site.


Середа, 8 травня 2013 р. 15:01:56 UTC+3 користувач lsPeter написав:
Hi Ngoc,

Ngoc Dao

unread,
Oct 9, 2013, 10:39:54 PM10/9/13
to xitrum-f...@googlegroups.com
About verbosity, I think Lift's view is OK. It has to be that way for the powerful "bind" feature. But for other parts, one example is too many imports:

Lift:

Xitrum:

About the "bind" feature, I'm planning to readd postback idea from Nitrogen/N2O:
Reply all
Reply to author
Forward
0 new messages