Module: Chef::Mixin::Template::ChefContext

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

Instance Method Summary collapse

Instance Method Details

#nodeObject



27
28
29
30
31
# File 'lib/chef/mixin/template.rb', line 27

def node
  return @node if @node
  raise "Could not find a value for node. If you are explicitly setting variables in a template, " +
        "include a node variable if you plan to use it."
end

#render(partial_name, options = {}) ⇒ Object

Takes the name of the partial, plus a hash of options. Returns a string that contains the result of the evaluation of the partial.

All variables from the parent template will be propagated down to the partial, unless you pass the variables option (see below).

Valid options are:

:local

If true then the partial name will be interpreted as the path to a file on the local filesystem; if false (the default) it will be looked up in the cookbook according to the normal rules for templates.

:source

If specified then the partial will be looked up with this name or path (according to the local option) instead of partial_name.

:cookbook

Search for the partial in the provided cookbook instead of the cookbook that contains the top-level template.

:variables

A Hash of variable_name => value that will be made available to the partial. If specified, none of the variables from the master template will be, so if you need them you will need to propagate them explicitly.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/mixin/template.rb', line 56

def render(partial_name, options = {})
  raise "You cannot render partials in this context" unless @template_finder

  if variables = options.delete(:variables)
    context = {}
    context.merge!(variables)
    context[:node] = @node
    context[:template_finder] = @template_finder
  else
    context = self.dup
  end

  template_location = @template_finder.find(partial_name, options)
  eruby = Erubis::Eruby.new(IO.read(template_location))
  output = eruby.evaluate(context)
end