# Dynamic creation of layouts at runtime

**URL:** <https://www.ruby-forum.com/t/dynamic-creation-of-layouts-at-runtime/193958>\
**Category:** Rails\
**Created:** [August 27, 2010, 6:57am UTC](https://www.ruby-forum.com/t/dynamic-creation-of-layouts-at-runtime/193958 "2010-08-27T06:57:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 27, 2010, 6:57am UTC](https://www.ruby-forum.com/t/dynamic-creation-of-layouts-at-runtime/193958/1 "2010-08-27T06:57:15Z")

</div>

I’ve learned how to dynamically change which layout is used at  
runtime, but I now have a question of whether it is possible to create  
new layouts (or modify existing ones) at runtime and have the app  
automatically recognize those changes? If so could you point me to an  
example or explain how it would be done.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [August 27, 2010, 11:14am UTC](https://www.ruby-forum.com/t/dynamic-creation-of-layouts-at-runtime/193958/2 "2010-08-27T11:14:49Z")

</div>

\*\*  
Le 27 aoÃ»t 2010 06:56:13 UTC+2, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a Ã©crit :

> I’ve learned how to dynamically change which layout is used at  
> runtime, but I now have a question of whether it is possible to create  
> new layouts (or modify existing ones) at runtime and have the app  
> automatically recognize those changes? If so could you point me to an  
> example or explain how it would be done.

Have you ever tried using partials? Partials can work you wonders. You  
need  
to have several partials (with varied layouts as you wish) and have one  
_main  
view_ that calls them and switch them depending on a given condition. I  
have  
a similar situation in some apps I work on. I give users different  
“dashborad” layouts depending on their roles.

Let me give you a practical example:

 \<%= render :partial =\> "details" %\> \<% if @super\_user || @doctor || @nurse%\> \<%= render :partial =\> "simple\_graph", :locals=\>{:fields=\>@patient} rescue nil%\> \<%end%\>

```
<%= render :partial => "patient_visit_summary" %>

```

 \<%if @doctor%\> \<%= render :partial =\> "doctors\_menu" %\> \<%elsif @clinician%\> \<%= render :partial =\> "clinicians\_menu" %\> \<%\<%elsif @nurse%\> \<%= render :partial =\> "nurses\_menu" %\> \<%\<%elsif @registration\_clerk%\> \<%= render :partial =\> "clerks\_menu" %\> \<%elsif @super\_user%\> \<%= render :partial =\> "super\_users\_menu" %\> \<%end %\> 

Please let us know if it works for you.

* * *

Edmond  
Software Developer | Baobab Health Trust ([http://www.baobabhealth.org/](http://www.baobabhealth.org/))  
|  
Malawi

Cell: +265 999 465 137 | +265 881 234 717

_“Many people doubt open source software and probably donâ€™t realize that  
there is an alternativeâ€¦ which is just as good…” – Kevin Scannell_
