1

How do I call a variable from my manifest inside, while of another variable in a template.erb file?

This is what I've tried to do:

<%= food[<%= menu %>] %>

How do I get this to work?

1
  • He means use a hash, which is definitely what you should do given the information provided. Commented Jun 3, 2016 at 19:21

1 Answer 1

3

You just use the variable, as is:

<%= food[menu] %>

You're already in "Ruby code space" within the ERB expansion, so you can use Ruby code as you normally would.

Referring to the Puppet 4.5 Embedded Ruby (ERB) template syntax section on Accessing Puppet Variables, there are 2 forms of variable access:

  • @variable syntax
  • scope['variable'] syntax

From the example in the question, there's not enough information to accurately determine the origin of the food variable. This question assumes that it's the result of processing an array or hash manifest variable. If food is the manifest variable, itself, it should be prefaced with @, as such:

<%= @food[menu] %>

If the manifest variable is foods, and the food variable is an element of the enumerable (Array or Hash), it would be used as such:

<%= @foods.each do |food| %>
  <%= @food[menu] %>
<% end %>

Stick with the documented methods of accessing the manifest variable, and you can use it in your template exactly as you would within traditional Ruby code.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. However, it is preferred for templates to access Puppet variables via the @var form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.