Method: Chef::Resource::Template#helpers

Defined in:
lib/chef/resource/template.rb

#helpers(module_name = nil, &block) ⇒ Object

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.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/chef/resource/template.rb', line 160

def helpers(module_name = nil, &block)
  if block_given? && !module_name.nil?
    raise Exceptions::ValidationFailed,
      "Passing both a module and block to #helpers is not supported. Call #helpers multiple times instead"
  elsif block_given?
    @inline_helper_modules << block
  elsif module_name.is_a?(::Module)
    @helper_modules << module_name
  elsif module_name.nil?
    raise Exceptions::ValidationFailed,
      "#helpers requires either a module name or inline module code as a block.\n" +
        "e.g.: helpers do; helper_code; end;\n" +
        "OR: helpers(MyHelpersModule)"
  else
    raise Exceptions::ValidationFailed,
      "Argument to #helpers must be a module. You gave #{module_name.inspect} (#{module_name.class})"
  end
end