Thank you very much for taking the time and giving me feedback.
Visitors are also reading...
← PreviousNext →

Implementing Base Templates In Play 2.0 Without Passing Arguments

16 Jan 2013

I just migrated from Java+Play1.0 to Java+Play2.0 and the templates are the hardest part so far, and the best way I found to implement a base template (for title, head etc..) is by using the Http.Context. There is a very nice syntax you can achieve with tags.

  \--- tags  

where get.scala.html is :


and set.scala.html is:


means you can write the following in any template

@import tags._  

So it is very readable and nice. This is the way I chose to go. stian - good advice. Proves it is important to scroll down to see all answers. :)

Passing HTML variables

I haven’t figured out yet how to pass Html variables.


however, I know how to pass them as block.


so you might want to replace set.scala.html with


this way you can pass Html blocks like so

     <meta description="something here">   

Side-Effect With My “Set” Implementation

A common use-case it template inheritance in Play. You have a base_template.html and then you have page_template.html that extends base_template.html. base_template.html might look something like

        <title> @context.get("title")</title>

while page template might look something like

    some page common context here..   

and then you have a page (lets assume login_page.html) that looks like

    login stuff..


The important thing to note here is that you set “body” twice. Once in “login_page.html” and then in “page_template.html”. It seems that this triggers a side-effect, as long as you implement set.scala.html like I suggested above.


as the page would show “login stuff…” twice because put returns the value that pops out the second time we put same key. (see put signature in java docs). scala provides a better way to modify the map


which does not cause this side effect.

← PreviousNext →