# Layouts - Header Integration

**URL:** <https://www.ruby-forum.com/t/layouts-header-integration/131180>\
**Category:** Rails\
**Created:** [March 6, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/layouts-header-integration/131180 "2008-03-06T18:33:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 6, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/layouts-header-integration/131180/1 "2008-03-06T18:33:01Z")

</div>

Basically, a layout wraps various snipits of html body-element code  
with a constant-content wrapper. Accordingly, it is the layout that  
contains the html element associated with the respective  
snipits and the element is the same for all such snipits. I  
seem to be encountering a variety of cases where I would like to have  
specific javascript code and/or styles (which I would ordinarily place  
in the html element) apply only to one specific html snipit of  
many such snipits that share a common element. I am wondering  
how others are approaching this situation. Thanks for any input.

```
     ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 6, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/layouts-header-integration/131180/2 "2008-03-06T19:05:33Z")

</div>

> Basically, a layout wraps various snipits of html body-element code  
> with a constant-content wrapper. Accordingly, it is the layout that  
> contains the html element associated with the respective  
> snipits and the element is the same for all such snipits. I  
> seem to be encountering a variety of cases where I would like to have  
> specific javascript code and/or styles (which I would ordinarily place  
> in the html element) apply only to one specific html snipit of  
> many such snipits that share a common element. I am wondering  
> how others are approaching this situation. Thanks for any input.

layout:

* * *
\<%= yield :page\_header %\> \<%= yield %\> ---------------------------------------------------

view:

* * *

\<% content\_for ‘page\_header’ do %\>  
javascript includes, stylesheet includes, raw javascript, etc.  
\<% end %\>

## blah blah

I’d also suggest having a “end\_of\_body” yield so you can easily insert  
javascript that must go right before the closing tag.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 6, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/layouts-header-integration/131180/3 "2008-03-06T23:37:48Z")

</div>

> ##

Perfect. Exactly what I wanted to know. Thanks! 🙂

```
  ... doug
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 7, 2008, 5:58am UTC](https://www.ruby-forum.com/t/layouts-header-integration/131180/4 "2008-03-07T05:58:31Z")

</div>

I was so lucky with the answer to my first question that I thought I  
would press further into a related aspect that is also troubling me.

I often want to launch a JavaScript when a particular page loads. The  
typical way that I do this is by including an onload event handler in  
the opening tag for the page. If the particular page in  
question happens to be formed by embedding a snippet of html code in a  
layout there may be a problem in that the body tag is typically in the  
layout and therefore shared with other pages similarly formed and for  
which I do not want to launch the JavaScript. Anyone have a good way  
of handling this?

Thanks.

```
     ... doug
```
