# Generic page element display initializer in a layout?

**URL:** <https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298>\
**Category:** Rails\
**Created:** [July 13, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298 "2006-07-13T18:32:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298/1 "2006-07-13T18:32:19Z")

</div>

All,

I’ve run into a snag. I need to handle the display of elements on my  
page and I want to use my Javascript event handlers on page load.

However, my BODY tag is trapped in my generic layout.

I’m thinking that I can put an onLoad on the BODY tag that will just  
call a Javascript method called initializePage() and then provide this  
initializePage() call in my specific page to call whatever handlers I  
need to in order to display things correctly.

Something in the back of my head is screaming AJAX but not sure how I  
would get that in.

Any thoughts?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298/2 "2006-07-13T19:04:13Z")

</div>

Wes G. wrote:

> All,
> 
> I’ve run into a snag. I need to handle the display of elements on my  
> page and I want to use my Javascript event handlers on page load.
> 
> However, my BODY tag is trapped in my generic layout.
> 
> I’m thinking that I can put an onLoad on the BODY tag that will just  
> call a Javascript method called initializePage() and then provide this  
> initializePage() call in my specific page to call whatever handlers I  
> need to in order to display things correctly.
> 
> Something in the back of my head is screaming AJAX but not sure how I  
> would get that in.
> 
> Any thoughts?
> 
> Wes

FYI, this kind of Javascript scheme will work, but I think I just need  
to make my template smarter, so I will do that instead.

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298/3 "2006-07-13T22:02:46Z")

</div>

On Jul 13, 2006, at 10:04 AM, Wes G. wrote:

> > this  
> > FYI, this kind of Javascript scheme will work, but I think I just need  
> > to make my template smarter, so I will do that instead.
> 
> WG

Wes-

```
You can also use the content_for method to push dynamic content

```

from your view up to the master layout.

So in your normal view:

\<% content\_for :javascript do %\>

 # blah blah more javascript \<% end %\> So in your layout: \<%= yield :javascript %\> -Ezra

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298/4 "2006-07-13T22:22:22Z")

</div>

Ezra Z. wrote:

> On Jul 13, 2006, at 10:04 AM, Wes G. wrote:
> 
> > > this  
> > > FYI, this kind of Javascript scheme will work, but I think I just need  
> > > to make my template smarter, so I will do that instead.
> > 
> > WG
> 
> Wes-
> 
> You can also use the content\_for method to push dynamic content  
> from your view up to the master layout.
> 
> So in your normal view:
> 
> \<% content\_for :javascript do %\>
> 
> # blah blah more javascript \<% end %\> So in your layout: \<%= yield :javascript %\> -Ezra

Whoa - so content\_for generates a block that can then be yielded to in  
any of the enclosing templates?

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298/5 "2006-07-13T23:52:42Z")

</div>

On Jul 13, 2006, at 1:22 PM, Wes G. wrote:

> > -Ezra
> 
> Whoa - so content\_for generates a block that can then be yielded to in  
> any of the enclosing templates?
> 
> WG

Wes-

```
Yeah, pretty sweet way to push dynamic content up from the partials

```

or templates into the layout. This is a very powerfull feature once  
you start playing with it. Makes your layouts much more flexible and  
reuseable because your normal views can specialize the layout.

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 13, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/generic-page-element-display-initializer-in-a-layout/98298/6 "2006-07-13T23:56:14Z")

</div>

Ezra Z. wrote:

> On Jul 13, 2006, at 1:22 PM, Wes G. wrote:
> 
> > > -Ezra
> > 
> > Whoa - so content\_for generates a block that can then be yielded to in  
> > any of the enclosing templates?
> > 
> > WG
> 
> Wes-
> 
> Yeah, pretty sweet way to push dynamic content up from the partials  
> or templates into the layout. This is a very powerfull feature once  
> you start playing with it. Makes your layouts much more flexible and  
> reuseable because your normal views can specialize the layout.
> 
> Cheers-  
> -Ezra

As I sometimes say when I get excited - that is f-in ballz!

🙂

WG
