Camping And ERB

The following is an oversimplified example of how to use ERB with Camping. "ERB provides an easy to use but powerful templating system for Ruby." ERB is used in Rails to create views.

Example

camping_with_erb.rb

# NOTE: Erb is slow. Please use Erubis for better performance.
begin
  require 'erubis'
  ERB = Erubis::Eruby
rescue
  require 'erb'
end

Camping.goes :Test

module Test
  def render(m, layout=true)
    content = ERB.new(IO.read("#{PATH}/templates/#{m}.html")).result(binding)
    content = ERB.new(IO.read("#{PATH}/templates/layout.html")).result(binding) if layout
    return content
  end
end

module Test::Controllers
  class Index < R '/(\w+)'
    def get(name)
      @name = name
      render :index
    end
  end
end

templates/layout.html

<html>
<body>
<%= content %>
</body>
</html>

templates/index.html

Hello <%= @name %>

References

ERB http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/