listing out available variables in an ERB template

M

mgerstenblatt

[Note: parts of this message were removed to make it a legal post.]

Hi All,

Suppose I have a Ruby ERB template named **my_template.html.erb**, and it
contains the following:

<div><%= @div_1 %></div>
<div><%= @div_2 %></div>
<div><%= @div_3 %></div>

Is there a way I can programatically list out all the available variables in
the template?

For example, the following method:

def list_out_variables
template = File.open("path_to/my_template.html.erb", "rb").read
erb = ERB.new( template )
erb.this_method_would_list_out_variables
end

would return something like:

['div1','div2','div3']


Any help would be greatly appreciated.

Thanks,
Mike
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

On Sun, Sep 26, 2010 at 8:12 PM, (e-mail address removed) <
Hi All,

Suppose I have a Ruby ERB template named **my_template.html.erb**, and it
contains the following:

<div><%= @div_1 %></div>
<div><%= @div_2 %></div>
<div><%= @div_3 %></div>

Is there a way I can programatically list out all the available variables
in
the template?

For example, the following method:

def list_out_variables
template = File.open("path_to/my_template.html.erb", "rb").read
erb = ERB.new( template )
erb.this_method_would_list_out_variables
end

would return something like:

['div1','div2','div3']


Any help would be greatly appreciated.

Thanks,
Mike


Ruby has some reflexive methods you can use.


$gvar = 1
lvar = 2
@ivar = 3
class Foo
@@cvar = 4
end

p instance_variables
p global_variables
p local_variables
class Foo
p class_variables
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top