nonopolarity
nonopolarity

Reputation: 151026

For Ruby and Rails, should a partial view be created when it is used only once?

I am thinking for partial view that is only used once, such as page header and footer, and the tabs in the header, they can be all embedded in the application layout, and all pages will have them.

So it is like programming -- if something is done only once, probably don't create a function to do it, to avoid overheads. But there might be situations where a function is more self-contained, modular, and make the code more clear to understand. In that case, we probably will create the function.

How about for partials? Should they be created, such as for header and footer, tabs, or sidebar selections. I am also thinking that when one day, if there is a page that doesn't include the header but has the footer, then the header will need to be made into a partial, so why not do it on day 1? But how does it impact the overhead of too many partials slowing down the page generation?

update: there is also the danger of too many partials... where a view includes a partial, and a partial includes a partial, and then again includes a partial. At this point, it is easy to be a bit lost.

Upvotes: 1

Views: 477

Answers (2)

Michael Koper
Michael Koper

Reputation: 9781

I always create those partials because i would like to have those things in seperate places. For example a Header partial and a footer partial are 2 seperate things both beginning with a div tag and closing with a div tag. For me it makes much more sense to have them in a seperate file. You keep your layout much cleaner.

In Rails 1 and 2 partials were a little slow (not even that much) In Rails 3 rendering partials is much faster.

Upvotes: 3

Ant
Ant

Reputation: 3887

I would think the overheads of including a partial are negligible.

If it makes your code neater / more organised / future-proof then I say do it.

Upvotes: 2

Related Questions