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:


${play.Play.configuration.get("fb.appId")}

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


%{
cf = play.Play.configuration
}%
${cf.get("fb.appId")}

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'}
GOOGLE ANALYTICS CODE - WILL ONLY DISPLAY IN PRODUCTION
#{/if}

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:
    play.configuration[‘your.property’]

Leave a Reply

Notice: Undefined variable: user_ID in /usr/share/nginx/html/wp-content/themes/getbetter/comments.php on line 65