chef
https://github.com/opscode/chef
Ruby
A systems integration framework, built to bring the benefits of configuration management to your entire infrastructure.
Chef::Resource::Template#helpers
Declares a module to define helper methods in the template's context
when rendering. There are two primary forms.
=== Inline Module Definition
When a block is given, the block is used to define a module which is
then mixed in to the template context w/ `extend`.
==== Inline Module Example
Given the following code in the template resource:
helpers do
# Add "syntax sugar" for referencing app-specific attributes
def app(attribute)
@node[:my_app_attributes][attribute]
end
end
You can use it in the template like so:
<%= app(:listen_ports) %>
Which is equivalent to:
<%= @node[:my_app_attributes][:listen_ports] %>
=== External Module Form
When a module name is given, the template context will be extended with
that module. This is the recommended way to customize template contexts
when you need to define more than an handful of helper functions (but
also try to keep your template helpers from getting out of hand--if you
have very complex logic in your template helpers, you should further
extract your code into separate libraries).
==== External Module Example
To extract the above inline module code to a library, you'd create a
library file like this:
module MyTemplateHelper
# Add "syntax sugar" for referencing app-specific attributes
def app(attribute)
@node[:my_app_attributes][attribute]
end
end
And in the template resource:
helpers(MyTemplateHelper)
The template code in the above example will work unmodified.Edit
git clone [email protected]:opscode/chef.git
cd chef
open lib/chef/resource/template.rb
Contribute
# Make a new branchgit checkout -b -your-name--update-docs-Chef--Resource--Template-helpers-for-pr
# Commit to gitgit add lib/chef/resource/template.rbgit commit -m "better docs for Chef::Resource::Template#helpers"
# Open pull requestgem install hub # on a mac you can `brew install hub`
hub fork
git push <your name> -your-name--update-docs-Chef--Resource--Template-helpers-for-pr
hub pull-request
# Celebrate!