Adam Speaks Out Of Turn

Posted on 2008/04/07

Dynamic layouts in merb

Mix and match layouts per controller in merb

This entry reflects an older version of a library or code samples that may have expired since the time of it’s writing. Investigate the library or code samples further before use.

To dynamically set a layout in a merb controller:

1 class Widgets < Application
2 
3   self._layout = condition ? 'true_layout' : 'false_layout'
4 
5 end
6 

If you want to wrap every request in a layout, try:

 1 class Application
 2 
 3   def dispatch(*args)
 4     self._layout = condition ? 'true_layout' : 'false_layout'
 5     super
 6   end
 7 
 8 end
 9 

Notice that you’ve got to assign a string to self._layout, rather than using a symbol.

This is equivalent to doing the following in a rails controller

1   layout :something

...this is likely to change in upcoming versions of merb, but if you need it now, this is how you’d do it.