# Helper or partial for tab creation

**URL:** <https://www.ruby-forum.com/t/helper-or-partial-for-tab-creation/71704>\
**Category:** Rails\
**Created:** [October 10, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/helper-or-partial-for-tab-creation/71704 "2006-10-10T22:55:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [October 10, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/helper-or-partial-for-tab-creation/71704/1 "2006-10-10T22:55:51Z")

</div>

I have this at the moment for creating my tabs, it works ok but was  
wondering if it would be faster to have a basic partial with a series of  
if statements ? Im just wondering if there are any overheads involved  
with generating lis this way.

```
#header navigation helper methods
  def main_tabs(controller)

   items = [ content_tag('li', link_to('Products', categories_path), 
:class => "page_item"),
              content_tag('li', link_to('Pages', pages_path), :class => 
"page_item"),
              content_tag('li', link_to('Mail', emails_path), :class => 
"page_item"),
              content_tag('li', link_to('Accounts', users_path), :class 
=> "page_item")]

    case controller
        when /categories|products|parts|accessories|actions/
            items[0] = content_tag('li', link_to('Products', 
categories_path), :class => "current_page_item")

        when 'pages'
            items[1] = content_tag('li', link_to('Pages', pages_path), 
:class => "current_page_item")

        when 'emails'
            items[2] = content_tag('li', link_to('Mail', emails_path), 
:class => "current_page_item")

        when 'users' && user.admin?
            items[3] = content_tag('li', link_to('Accounts', 
users_path), :class => "current_page_item")
    end

    content_tag('ul', items.to_s, :class=>'menu')
  end
```

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [October 11, 2006, 1:35am UTC](https://www.ruby-forum.com/t/helper-or-partial-for-tab-creation/71704/2 "2006-10-11T01:35:02Z")

</div>

Adam wrote:

> I have this at the moment for creating my tabs, it works ok but was  
> wondering if it would be faster to have a basic partial with a series of  
> if statements ? Im just wondering if there are any overheads involved  
> with generating lis this way.

I think that Rails sort of compiles RHTML templates when they’re loaded.  
From ActiveRecord::Base doc:

“By default, Rails will compile each template to a method in order to  
render it. When you alter a template, Rails will check the fileâ??s  
modification time and recompile it.”

I think therefore that any static HTML in the template is converted into  
literal strings in this new method that is created on compilation. So if  
your partial has static HTML for the tabs at all, that would definitely  
be faster than executing your current code every time the helper is  
called.

Also if the contents of your ‘items’ array is not dependent on any  
instance variables, you could define its contents at class definition  
time (although you may not be able to use #link\_to and #content\_tag  
then), as a class variable. Then that part of the code only gets  
executed once. Just make sure you don’t overwrite its elements like you  
do currently.
