Iterate over all the variables in an erb template

Is there any way to iterate or access all the variables passed by the template resource in a ERB template?

Example: Let say I have this template definition:

template "path/name" do
    source 'source.erb'
    action :create
    variables(
      var1: 'var1',
      var2: 'var2',
      var3: 'var3',
     ...
    )
end

Can I access all of them doing something similar to the below instead of using the variable names individually?

@variables.each do |var|
...
end

Thanks!

Pass in var which is array or hash and iterate through that

It is possible, but not in a way you won’t regret later. If you really must know, ruby has an instance_variables method that will give you the list, and an instance_variable_get method that lets you access them without using literals. That said, templates have some instance variables that Chef Client sets behind the scenes for bookkeeping, and the names and existence or non-existence of those can change at any time. Plus, what you’re suggesting would break if you ever added a different variable that wasn’t conceptually part of the list.

Per the previous reply, the best thing to do is pass your variable in as a collection of some sort and then iterate over that.

Great feedback, thank you guys.

Yeah I should probably go with the collection option.