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
_______________________________________________
Camping-list mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/camping-list

Reply via email to