# How can I set controller-wide layout and template?

**URL:** <https://www.ruby-forum.com/t/how-can-i-set-controller-wide-layout-and-template/112764>\
**Category:** Rails\
**Created:** [August 23, 2007, 12:48am UTC](https://www.ruby-forum.com/t/how-can-i-set-controller-wide-layout-and-template/112764 "2007-08-23T00:48:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 23, 2007, 12:48am UTC](https://www.ruby-forum.com/t/how-can-i-set-controller-wide-layout-and-template/112764/1 "2007-08-23T00:48:34Z")

</div>

Hi,

When you want to change the default layout and template, you can do the  
following.

render :layout =\> “some\_controller/my\_layout” :template =\> “my\_template”

I’m in a situation where I need to change the layout and template  
dynamically according to the data from DB.  
I want to set them for controller-wide instead of action-wide.  
(In one place instead of many places)  
How can I do that?

For example,

In view/boards/one, there are show.rhtml, list.rhtml, …  
In view/boards/two, there are show.rhtml, list.rhtml, …

I want to change the location of templates dynamically.  
This is sort of like theme change.

Layouts have the same issue.

Thanks in advance.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 23, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/how-can-i-set-controller-wide-layout-and-template/112764/2 "2007-08-23T20:30:38Z")

</div>

in your controller:

layout :choose\_layout

def choose\_layout  
if Time.now.hour == 14  
“two\_pm\_layout”  
else  
“other\_layout”  
end  
end

Adam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 23, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/how-can-i-set-controller-wide-layout-and-template/112764/3 "2007-08-23T20:36:09Z")

</div>

Adam C. wrote:

> in your controller:
> 
> layout :choose\_layout
> 
> def choose\_layout  
> if Time.now.hour == 14  
> “two\_pm\_layout”  
> else  
> “other\_layout”  
> end  
> end
> 
> Adam

Hi Adam,

Thanks for the answer.  
Do you know how to dynamically change the location of template?  
The template file name will be decided by the action name but the  
location(directory) of the file should be dynamic.

Sam
