# Application layout question

**URL:** <https://www.ruby-forum.com/t/application-layout-question/106494>\
**Category:** Rails\
**Created:** [June 11, 2007, 10:18am UTC](https://www.ruby-forum.com/t/application-layout-question/106494 "2007-06-11T10:18:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![springbok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@springbok](https://www.ruby-forum.com/u/springbok)\
**Post date:** [June 11, 2007, 10:18am UTC](https://www.ruby-forum.com/t/application-layout-question/106494/1 "2007-06-11T10:18:14Z")

</div>

Hi,

I’m a RoR noob so excuse me if this is an obvious question. I’ve  
looked in all my RoR books (I have 5) and tried to find the answer in  
the newsgroups but to no avail.

I have an application layout which defines the layout of the site  
which has a left and right column and a content column in the middle  
(3 col layout).

The left/right column contents can change depending on the current  
action, so for example for my login form I do not want anything  
displayed in the left but I do for the right. Once logged in I want  
something in both columns, the content changes as the user navigates  
the site.

My thoughts where to use partials so that the view is responsible for  
what gets displayed in these columns. I would have something like this  
in the application layout:

```
        <div id="right">
             <%= render (:partial => "rightcolumn") %>
        </div> <!-- right -->

```

I would then define \_righcolumn.rhtml for each view.

Is this the correct approach, in those cases where I do not display  
anything in either column would I have to include a blank partial???

Thanks.

---

<div class="post-metadata">

**Author:** ![springbok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@springbok](https://www.ruby-forum.com/u/springbok)\
**Post date:** [June 11, 2007, 11:26am UTC](https://www.ruby-forum.com/t/application-layout-question/106494/2 "2007-06-11T11:26:23Z")

</div>

# in layout file (For ex. application.rhtml)

 \<%= yield :right %\>

 \<%= yield :left %\>

# in any view (\*.rhtml) that you want to fill the div…

\<% content\_for :right do -%\>

\<% end -%\>

\<% content\_for :left do -%\>

\<% end -%\>

HTH

- H

On 6/11/07, springbok [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> <div id="right">
> 
> ```
> 
> >

–  
Husein C.  
Yucca Intelligence Development

> **[قفل دیجیتال | قفل هوشمند | قفل الکترونیکی کره‌ای | یوکا](https://yuccahq.com/)**
>
> نگران قیمت بی‌ثبات هستی؟ از تخصصی‌ترین برند قفل هوشمند با اطمینان و تضمین قیمت خرید کن! معتبرترین برند کره جنوبی:قفل الکترونیکی یوکا همین الان تماس بگیرید و از تخفیف‌های شگفت‌انگیز ما استفاده کنید.

---

<div class="post-metadata">

**Author:** ![springbok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@springbok](https://www.ruby-forum.com/u/springbok)\
**Post date:** [June 11, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/application-layout-question/106494/3 "2007-06-11T18:23:48Z")

</div>

That is one approach. The downside is that you must provide the  
partial for every view.

If on the other hand you allow the controller to name the partials you  
can share partials between views. For example:

@right\_partial = “shared/shared\_view1”

Which references view shared\_view1 in the shared directory.

Michael

---

<div class="post-metadata">

**Author:** ![springbok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@springbok](https://www.ruby-forum.com/u/springbok)\
**Post date:** [June 12, 2007, 4:57am UTC](https://www.ruby-forum.com/t/application-layout-question/106494/4 "2007-06-12T04:57:25Z")

</div>

Thanks everyone for your suggestions, I’ve taken Michael’s approach as  
I think it will best suit my needs. An added bonus is that I will be  
able to share partials in those cases where the same content is  
displayed in the left/right columns.

---

<div class="post-metadata">

**Author:** ![springbok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@springbok](https://www.ruby-forum.com/u/springbok)\
**Post date:** [June 11, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/application-layout-question/106494/5 "2007-06-11T18:50:53Z")

</div>

I suspect you may want to use different layouts depending on the  
circumstance. You may also me interested in the nested layouts  
plugin([http://agilewebdevelopment.com/plugins/nested\_layouts](http://agilewebdevelopment.com/plugins/nested_layouts)), I’ve  
found it quite useful for these types of things.

good luck!  
Tim

---

<div class="post-metadata">

**Author:** ![springbok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@springbok](https://www.ruby-forum.com/u/springbok)\
**Post date:** [June 12, 2007, 6:07am UTC](https://www.ruby-forum.com/t/application-layout-question/106494/6 "2007-06-12T06:07:29Z")

</div>

I also use conditions in my layout for this kinda stuff a bit, stuff  
like

\<% unless @user.logged\_in==true %\>  
… do some stuff  
\<% else %\>  
… do some other stuff  
\<% end %\>

Doesn’t feel as “Railsy” but it’s a darn site easier to read and  
debug.

Cam

---

<div class="post-metadata">

**Author:** ![springbok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@springbok](https://www.ruby-forum.com/u/springbok)\
**Post date:** [June 12, 2007, 12:59pm UTC](https://www.ruby-forum.com/t/application-layout-question/106494/7 "2007-06-12T12:59:35Z")

</div>

I’m sure this is known, but just to take Michael’s example one simple  
step further you could add an if statement to only render if the  
@right\_partial varible has been set.

\<div id="right\> \<% if @right\_partial %\> \<%= render :partial =\> @right\_partial %\> \<% else %\> \<% end %\> \</div\>
