Monday, January 16, 2012

Accessing Dancer config settings inside of a template

As I continue to work more and more with Perl Dancer, I run into situations where I need to pull in application configuration data inside of the template system.

Dancer provides access to this config in your template system with the template token settings. What caught me the other day was my desire to use it in the same way as the in the app's DSL: setting. This was my misreading of the documentation. To access settings in Template Toolkit, you must grab sub areas as hash keys.

Example: (you can see this in the generated new app code)
App name: [% settings.appname %]
I did have a case where I wanted to pull in a value that was located in the plugin section. The plugin (like some of them) are named with double colons (::) and I couldn't get the hash key lookup to work so I used TT's item virtual method.

This example dumps out RSS version:
Using RSS Output Version: 
[% settings.plugins.item('XML::RSS').output %]

Disclaimer: Of course in the case of some configuration values it might make more sense to update the plugin code to share the value or add a before template hook to pass it out.

__END__

1 comment: