# Sitecontroller instance variable in custom tag

**URL:** <https://www.ruby-forum.com/t/sitecontroller-instance-variable-in-custom-tag/123051>\
**Category:** Radiant CMS\
**Created:** [December 13, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/sitecontroller-instance-variable-in-custom-tag/123051 "2007-12-13T17:37:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/sitecontroller-instance-variable-in-custom-tag/123051/1 "2007-12-13T17:37:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/sitecontroller-instance-variable-in-custom-tag/123051/2 "2007-12-13T17:58:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/sitecontroller-instance-variable-in-custom-tag/123051/3 "2007-12-13T19:06:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/sitecontroller-instance-variable-in-custom-tag/123051/4 "2007-12-13T22:27:12Z")

</div>

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!
