# Best practice for modifying html header in templates

**URL:** <https://www.ruby-forum.com/t/best-practice-for-modifying-html-header-in-templates/92377>\
**Category:** Rails\
**Created:** [April 18, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/best-practice-for-modifying-html-header-in-templates/92377 "2007-04-18T21:25:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [April 18, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/best-practice-for-modifying-html-header-in-templates/92377/1 "2007-04-18T21:25:54Z")

</div>

In my rails app, I have a few resources whose header needs differ from  
the rest of the resources - they need extra javascript libraries, or  
different css stylesheets, or link to an RSS feed to which I’d also like  
to link in the html header. All pages share a standard layout template,  
of course. I’m wondering what the best practice is for adding extra  
stuff to the html header for individual pages. Currently, I populate  
@extra\_javascripts and @extra\_stylesheets variables in the page  
templates that require extra love, and check for the existence of those  
variables in the master layout and add the links as necessary. I could  
do this again for my rss feed, but this strikes me as a bit smelly. Has  
anyone tackled a better approach to this?

- donald

---

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [April 18, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/best-practice-for-modifying-html-header-in-templates/92377/2 "2007-04-18T21:58:57Z")

</div>

> anyone tackled a better approach to this?  
> In your views do something like this:

\<% content\_for(“page\_header”) do -%\>  
\<%= javascript\_include\_tag ‘my\_special\_js’ %\>  
…  
\<% end -%\>

Then in your layout do this:

…

... \<%= yield "page\_header" %\> ...

We do this and it works quite well. Another nice one is  
“page\_ending\_javascript” for those odd cases where the javascript has to  
go at the end of the page just before the closing .

-philip

---

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [April 18, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/best-practice-for-modifying-html-header-in-templates/92377/3 "2007-04-18T22:20:01Z")

</div>

> ... \<%= yield "page\_header" %\> ...

That’s _exactly_ the elegant solution I was hoping existed. Many thanks.

- donald

---

<div class="post-metadata">

**Author:** ![BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@BallS\_Donald\_A\_Jr\_S](https://www.ruby-forum.com/u/BallS_Donald_A_Jr_S)\
**Post date:** [April 19, 2007, 2:13am UTC](https://www.ruby-forum.com/t/best-practice-for-modifying-html-header-in-templates/92377/4 "2007-04-19T02:13:19Z")

</div>

On 18-Apr-07, at 4:18 PM, Ball, Donald A Jr ((Library)) wrote:

> > …

That’s a really cool solution Philip. Learn something new very other  
day.

thanx.  
Jodi
