DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Hans has posted 19 posts at DZone. View Full User Profile

The Camping Short, Short Example

03.28.2006
| 7468 views |
  • submit to reddit
        Camping encourages short, elegant applications. In this example, we're going to skip the database and put together a simple home page with a few of your favorite links.

The site can be stored in a single file called home_page.rb:

 #!/usr/local/bin/ruby -rubygems
 require 'camping'

 module Camping::Controllers

   # The root slash shows the `index' view.
   class Index < R '/'
     def get
       render :index 
     end
   end

   # Any other page name gets sent to the view
   # of the same name.
   #
   #   /index -> Views#index
   #   /sample -> Views#sample
   #
   class Page < R '/(\w+)'
     def get(page_name)
       render page_name
     end
   end

 end

 module Camping::Views

   # If you have a `layout' method like this, it
   # will wrap the HTML in the other methods.  The
   # `self << yield' is where the HTML is inserted.
   def layout
     html do
       title { 'My HomePage' }
       body { self << yield }
     end
   end

   # The `index' view.  Inside your views, you express
   # the HTML in Ruby.  See http://code.whytheluckystiff.net/markaby/.
   def index
     p 'Hi my name is Charles.'
     p 'Here are some links:'
     ul do
      li { a 'Google', :href => 'http://google.com' }
      li { a 'A sample page', :href => '/sample' }
     end
   end

   # The `sample' view.
   def sample
     p 'A sample page'
   end
 end

 if __FILE__ == $0
   puts Camping.run
 end

Source: <a href="http://code.whytheluckystiff.net/camping/wiki/TheCampingShortShortExample#TheCampingShortShortExample">The Camping Short, Short Example</a>