So I like using ERB for views and have it implemented something like this: render t content = ERB.new(File.read "./some_app/views/#{t}.erb").result binding ERB.new(File.read "./some_app/views/layout.erb").result binding end So, effectively I have no Views module. However, I''d like to make use of some partials as well. I can make a method for every partial and throw it in the Helpers module but that doesn''t seem like the right road. Does anyone have any suggestions? I tried creating a partial method in the main module: def partial p ERB.new(File.read "./some_app/partials/#{p}.erb").result binding end And then tried overriding missing_method to be fancy: def missing_method symbol, args* p = symbol.to_s if p[0] == ''_'' partial p else super end end So I could call partials like this ''_some_partial'' but as the Controller is calling render, I would have to do this in every controller class I think. Does anyone have other suggestions? -- Dave
This is what I came up with in the end: module Helpers def method_missing symbol, *args unless /^_/!~symbol.to_s partial symbol.to_s else super end end def partial p ERB.new(File.read "./crud/views/#{p}.erb").result(binding) end end I took a look at the route maker, and rather than modify that to have all controllers inherit a Partials module I just added the above to the Helpers module. Design wise it''s not perfect. Partials really belong in the Views module, but with no equivalent to the Mab object when using ERB I couldn''t figure out a way to access partials from the Views module. Dave On Tue, Jun 16, 2009 at 3:18 PM, David Susco<dsusco at gmail.com> wrote:> So I like using ERB for views and have it implemented something like this: > > render t > ?content = ERB.new(File.read "./some_app/views/#{t}.erb").result binding > ?ERB.new(File.read "./some_app/views/layout.erb").result binding > end > > So, effectively I have no Views module. However, I''d like to make use > of some partials as well. I can make a method for every partial and > throw it in the Helpers module but that doesn''t seem like the right > road. Does anyone have any suggestions? I tried creating a partial > method in the main module: > > def partial p > ?ERB.new(File.read "./some_app/partials/#{p}.erb").result binding > end > > And then tried overriding missing_method to be fancy: > > def missing_method symbol, args* > ?p = symbol.to_s > ?if p[0] == ''_'' > ? ?partial p > ?else > ? ?super > ?end > end > > So I could call partials like this ''_some_partial'' but as the > Controller is calling render, I would have to do this in every > controller class I think. Does anyone have other suggestions? > > -- > Dave >-- Dave
It''s been a little while since I''ve coded in Ruby, but here''s a way to use alternative templating systems with Camping. #!/usr/bin/env ruby require ''rubygems'' require ''camping'' require ''erb'' Camping.goes :Alternative module Alternative::Controllers class Home < R ''/'' def get @name = "beppu" render :home end end end module Alternative::Views def layout self << "top\n" self << yield self << "bottom\n" end def method_missing(sym, *args, &block) template_path = "#{sym.to_s}.erb" if File.readable? template_path self << ERB.new(File.read(template_path)).result(binding) else # The view code must coexist with Markaby. # I don''t see an easy way around this, # because the render method assumes Markaby is being used. super end end end The key is to override *method_missing* in the Views module. In this example, instead of going straight to Markaby, it''ll first check the filesystem to see if a matching template exists. If so, it''ll run it through the templating system of your choice. The benefit of this approach is that the various conventions used by Camping are still in effect. - You can still call *render* :template from your controllers. - If the template''s name has a leading ''_'', it''s still treated as a partial template. - Otherwise, the content will be wrapped with the *layout* method if it exists. - Last but not least, your view code is still in the Views module. In this example, I used ERB, but you can generalize this solution to fit any templating system. --beppu -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/camping-list/attachments/20090619/923a7a88/attachment.html>
It has a lot of similarities to what you were already doing, but this integrates w/ Camping a little more smoothly. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/camping-list/attachments/20090619/bbecf842/attachment.html>