Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   listing out available variables in an ERB template (http://www.velocityreviews.com/forums/t864581-listing-out-available-variables-in-an-erb-template.html)

mgerstenblatt@gmail.com 09-27-2010 01:12 AM

listing out available variables in an ERB template
 
[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


Josh Cheek 09-27-2010 03:51 AM

Re: listing out available variables in an ERB template
 
[Note: parts of this message were removed to make it a legal post.]

On Sun, Sep 26, 2010 at 8:12 PM, mgerstenblatt@gmail.com <
mgerstenblatt@gmail.com> wrote:

> 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



All times are GMT. The time now is 10:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.