How to List the Available Variables in an Ruby Erb Template

Is there a way to list the available variables in an Ruby ERB template?

To get a list of variables available to your .erb file (from the controller):

Add a breakpoint in the erb:

<% debugger %>

Then type instance_variables in the debugger to see all of the available instance variables.

Added: Note that instance_variables is a method available from the Ruby class Object and all of its subclasses. (As noted by @mikezter.) So you could call the method programmatically from within your sw rather than using the debugger if you really wanted to.

You'll get back a list of instance variables for the current object.

Added: To get a list of the variables used by an .erb file:

# <template> is loaded with the entire contents of the .erb file as
# one long string
var_array = template.scan(/(\@[a-z]+[0-9a-z]*)/i).uniq

Ruby templating: is it possible that only pass some values to variables in template?

You can use double opening %%s to prevent it from being evaluated in erb.

This solution might look ugly but it returns exactly what you want (if it is what you want):

require 'erb'

template = ERB.new <<-EOF
The value of x is: <% if defined?(x) %><%= x %><% else %><%%= x %><% end %>
The value of y is: <% if defined?(y) %><%= y %><% else %><%%= y %><% end %>
EOF
=> #<ERB:0x00007feeec80c428 @safe_level=nil, @src="#coding:UTF-8\n_erbout = +''; _erbout.<<(-\" The value of x is: \"); if defined?(x) ; _erbout.<<(( x ).to_s); else ; _erbout.<<(-\"<%= x %>\"); end ; _erbout.<<(-\"\\n The value of y is: \"\n); if defined?(y) ; _erbout.<<(( y ).to_s); else ; _erbout.<<(-\"<%= y %>\"); end ; _erbout.<<(-\"\\n\"\n); _erbout", @encoding=#<Encoding:UTF-8>, @frozen_string=nil, @filename=nil, @lineno=0>

puts template.result(binding)
The value of x is: <%= x %>
The value of y is: <%= y %>
=> nil

x = 20

puts template.result(binding)
The value of x is: 20
The value of y is: <%= y %>
=> nil

y= 50

puts template.result(binding)
The value of x is: 20
The value of y is: 50
=> nil

I assume you can do the formatting.

Using variables in ruby erb variables

Edit: An easier way would be to make a normal array instead of a percent literal. [@addhostgroup, @addservicegroup].each do |action|. Sometimes I over look the little things :)

You can use instance_variable_get along with the array and iteration into action that you have setup.

Add a line immediately inside the each block with:
<% instance_var_value = instance_variable_get(:"@#{action}") %>

You could also map the values into the array before iterating over it.

Ruby templates: How to pass variables into inlined ERB?

Got it!

I create a bindings class

class BindMe
def initialize(key,val)
@key=key
@val=val
end
def get_binding
return binding()
end
end

and pass an instance to ERB

dataHash.keys.each do |current|
key = current.to_s
val = dataHash[key]

# here, I pass the bindings instance to ERB
bindMe = BindMe.new(key,val)

result = template.result(bindMe.get_binding)

# unnecessary code goes here
end

The .erb template file looks like this:

Key: <%= @key %>

Rails erb template list all functions

Exactly same as instance variables

<%= methods %>

in your rails html.erb file

Chef ERB template variable in a .each loop

The solution:

<%
zookeeper_connection_string = ""
@zookeeper_machines.each do |name|
zookeeper_connection_string = zookeeper_connection_string + "#{name},"
end
zookeeper_connection_string = zookeeper_connection_string.chomp(",")
%>
zookeeper.connect=<%= zookeeper_connection_string %>

The end result is:

zookeeper.connect=kafka01:2181,kafka02:2181,kafka03:2181

View all instance variables currently available? (Ruby)

@foo = 3
instance_variables # => [:@foo]

create instance variable in erb template

I guess you might want to make that show view a partial. Something like:

<h2><%= vehicle.type %></h2>
<ul>
<% vehicle.accessories.each do |accessory| %>
<li><% accessory.title %></li>
<% end %>
</ul>

Then you can do something like:

<% @vehicles.each do |vehicle| %>
<%= render "path/to/show_partial", locals: {vehicle: vehicle} %>
<% end %>

Naturally, you'll want to:

  • use the real path/to/show_partial, whever that happens to be, and
  • do some eager loading so that you're not doing a whole bunch of N+1 queries.


Related Topics



Leave a reply



Submit