[1.2.4] Access application.conf from Javascript file

1,152 views
Skip to first unread message

Nicolas Forney

unread,
May 12, 2012, 10:05:18 AM5/12/12
to play-fr...@googlegroups.com
Hi there,

I'd like to know if there is an easy way to access application.conf properties from a Javascript file (separated from the template file)?

I use the #{i18n/} tag which creates a "i18n" variable accessible from external Javascript files. As there is no {#config/} tag I'd like to know if any of you have another simple solution?

Thx

Ivan San José García

unread,
May 12, 2012, 11:17:58 AM5/12/12
to play-fr...@googlegroups.com
${play.configuration.get("key")}

2012/5/12 Nicolas Forney <chil...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/6gKvLa1OgrAJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Nicolas Forney

unread,
May 13, 2012, 12:31:57 PM5/13/12
to play-fr...@googlegroups.com
This command is only accessible in templates files parsed by the framework. External javascript files are not parsed so the "${play.configuration.get("key")}" can not be used.

On Saturday, May 12, 2012 5:17:58 PM UTC+2, ivansjg wrote:
${play.configuration.get("key")}

2012/5/12 Nicolas Forney <chil...@gmail.com>
Hi there,

I'd like to know if there is an easy way to access application.conf properties from a Javascript file (separated from the template file)?

I use the #{i18n/} tag which creates a "i18n" variable accessible from external Javascript files. As there is no {#config/} tag I'd like to know if any of you have another simple solution?

Thx

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/6gKvLa1OgrAJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Marcel Klemenz

unread,
May 14, 2012, 4:39:47 AM5/14/12
to play-fr...@googlegroups.com
Bad idea:
You could add a route, download the file via ajax and parse it, but you should not: Anyone could read your database connection properties (user/pw/host) and your secret key (anyone could decrypt your crypted data, write own cockies which are accepted by play)
this would work like this:

GET     /appconf                                    staticFile/conf/application.conf

realy, don't do it!

Better idea:
Write a controller method that reads your Play.configuration and adds only the keys you really need to a Map or whatever and deliver it as json. Should be easier to use in JS, too.

Peter Hilton - committer (Lunatech)

unread,
May 14, 2012, 6:23:12 AM5/14/12
to play-fr...@googlegroups.com
My preferred solution, which is clean and simple, is to use HTML data attributes in the template:

<body data-key="${play.configuration.get("key")}">

and then use the jQuery data API to get the value from a script:

var key = $('body').data('key');

The nice thing about this approach its the de-coupling, which means that your JavaScript can stay in a separate file, and doesn't need to know how the configuration data got into the DOM. Note that it is sometimes appropriate to use a more specific place in the DOM than the body tag.


Nicolas Forney

unread,
May 14, 2012, 6:31:08 AM5/14/12
to play-fr...@googlegroups.com
Both approaches are interesting. Peter, I already use your HTML attribute method. I read previous posts you wrote ;); so I will stick to this method.

Thank you both for your help.

Regards.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/zc2k4nOc4WwJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages