Always Get Better

Accessing Configuration Parameters using Play Framework’s Template Engine

Suppose we are building a Facebook application and have a variable called fb.appId containing our Application’s ID. We want to use that to initialize an FBJS call, but obviously don’t want to hard-code it into our page’s template.

Using the template engine in Play! framework, we can make direct calls to the underlying Java framework as long as we use the full namespace path. Our Facebook Application Id is accessible like this:


If we’re making a lot of configuration calls, we can simplify our lives by aliasing the namespace path:

cf = play.Play.configuration

Conditional Statements
Let’s go a step further. Suppose we have Google Analytics and only want the JavaScript to run when our site has been deployed to production (I covered a similar technique earlier this year using Ruby on Rails).

#{if play.Play.configuration.get("application.mode") == 'PROD'}

Tags: , ,

One Response to “Accessing Configuration Parameters using Play Framework’s Template Engine”

  1. a says:

    in play 1.2 variable ‘play’ is implicitly available in templates. so you can just write:

Leave a Reply