Sitecontroller instance variable in custom tag

Hi,

I’m trying to figure out how to access the value of an instance variable
that gets setup on SiteController when my extension is activated. I’d
like to use this value in a tag, but I can’ figure out how to access
that variable from the tag.

If I setup a tag that just dumps the page.inspect, like the tag below, I
can see the instance variable in the output, but I just can’t figure out
how to grab it.


tag ‘inspect_page’ do |tag|
tag.globals.page.inspect
end

I’d like to be able to do something like


tag ‘my_instance_variable’ do |tag|
# @my_instance_variable is defined in SiteController on extension
activation
tag.globals.page.my_instance_variable
end

Anyone know how to do this?

Thanks!

Dave,

You probably need to explicitly set the instance variable on the page by
overriding process_page. Here’s a sample (untested):

Page.send :attr_accessor, :my_instance_variable

SiteController.class_eval do
def process_page_with_instance_var(page)
page.my_instance_variable = @my_instance_variable
process_page_without_instance_var(page)
end
alias_method_chain :process_page, :instance_var
end

From that, your instance variable would be defined on the global page
(tag.globals.page).

I’ve been looking through the Rails source, but I don’t see anywhere
where the request is given a link back to the controller it’s created
for. You may be out of luck there.

Cheers,

Sean

You may want to use Ruby’s instance_variable_get(…) like this:

tag ‘my_instance_variable’ do |tag|
tag.globals.page.instance_variable_get(’@my_instance_variable’)
end

I ended up getting it working with the exact method that Sean suggested.
Not bad for untested code, Sean. Thanks!

Todd, it turns out that the instance_variable_get wasn’t necessary
because my_instance_variable was setup with an accessor via:
Page.send :attr_accessor, :my_instance_variable

This made it accessible as:
tag ‘my_instance_variable’ do |tag|
tag.globals.page.my_instance_variable
end

Thanks again!