public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Added support for dynamic layouts.

When a layout is specified as a symbol on the controller level, we now try to 
call
a method with that name to dynamically determine the layout. We fall back to
interpreting the symbol as the name of the layout if no method with that name 
exists.
This is mainly to support rendering a different layout based on request 
parameters,
for example mobile vs. web. See ticket #66.

Signed-off-by: Michael D. Ivey <ivey@gweezlebur.com>
digitalhobbit (author)
Sat Feb 16 00:10:12 -0800 2008
ivey (committer)
Sun Feb 17 07:52:08 -0800 2008
commit  6be99f54af744cc84d3403cff5e11086b7f7496b
tree    d38d91a1a2a15554bb5d816e6783079790baefee
parent  3f0f4f410f29b14b9f72057fb91755fe402c6ef1
...
249
250
251
252
 
 
 
253
254
255
...
249
250
251
 
252
253
254
255
256
257
0
@@ -249,7 +249,9 @@ module Merb::RenderMixin
0
   #   raised if no layout was specified, and the default layouts were
0
   #   not found.
0
   def _get_layout(layout = nil)
0
-    layout = _layout.to_s if _layout    
0
+    if _layout && !layout
0
+      layout = _layout.instance_of?(Symbol) && self.respond_to?(_layout, true) ? send(_layout) : _layout
0
+    end
0
     layout = layout.to_s if layout
0
     
0
     # If a layout was provided, throw an error if it's not found
...
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
0
@@ -26,6 +26,18 @@ module Merb::Test::Fixtures
0
     class RenderStringControllerLayout < RenderString
0
       self._template_root = File.dirname(__FILE__) / "alt_views"
0
     end
0
+    
0
+    class RenderStringDynamicLayout < RenderString
0
+      layout :determine_layout
0
+      
0
+      def alt_index
0
+        render "the alt index"
0
+      end
0
+      
0
+      def determine_layout
0
+        action_name.index('alt') == 0 ? 'alt' : 'custom'
0
+      end
0
+    end
0
 
0
     class RenderTemplate < Testing
0
       
...
22
23
24
 
 
 
 
 
25
26
27
...
22
23
24
25
26
27
28
29
30
31
32
0
@@ -22,6 +22,11 @@ describe Merb::AbstractController, " rendering plain strings" do
0
     dispatch_should_make_body("RenderStringControllerLayout", "Controller: the index")
0
   end
0
 
0
+  it "should support rendering plain strings with dynamic layouts" do
0
+    dispatch_should_make_body("RenderStringDynamicLayout", "Custom: the index", :index)
0
+    dispatch_should_make_body("RenderStringDynamicLayout", "Alt: the alt index", :alt_index)
0
+  end
0
+  
0
 end
0
 
0
 describe Merb::AbstractController, " rendering templates" do

Comments