RSS
17 Nov 2007

Sinatra: Classy web-development dressed in a DSL

Author: ceefour | Filed under: Books, Cool, HTML, Rails, Ruby, Tools, Tutorials, Web 2.0

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

logo Sinatra: Classy web development dressed in a DSL

Sinatra is a new cool open-source DSL-driven web application framework!

This super-sexy DSL runs at lighting speed. It sits on top of Mongrel and was written to be thread-safe, sleek and tiny. And an entire web-application can be written and contained in one file (or a small collection of files)!

It’s super easy to use! Let’s create an app from scratch to demonstrate!

Install!

gem install sinatra -y

Use!

  • Create a file called lyrics.rb (or any name you like)
  • Add
      require 'rubygems'
    
      require 'sinatra'
  • Run (yes, with just ruby)
      % ruby lyrics.rb
    
      == Sinata has taken the stage on port 4567!
  • Take a moment and view the default page localhost:4567. Go ahead and bask in it‘s glory.
  • Notice:
    • It didn‘t create any page to show you that default page (just a cool thing to see, that‘s all)
    • There was nothing generated other than a log file
    • Sinatra is a really cool name for a web-framework that‘s a DSL
  • Modify lyrics.rb by adding:
      get '/' do
    
        'Hello World'
    
      end
  • Refresh (no need to restart Sinatra):
      http://localhost:4567
  • Modify again (then refresh):
  •   get '/' do
    
        <<-HTML
    
          <form action='/' method="POST">
    
            <input type="text" name="name" />
    
            <input type="submit" value="Say my name!" />
    
          </form>
    
        HTML
    
      end  post '/' do
    
        "Hello #{params[:name] || 'World'}!"
    
      end
  • Now you try: Use the Sinatra::Erb::EventContext or Sinatra::Haml::EventContext to do the same. Do them inline and as template files.
  • Learn more cool stuff: see Sinatra::Dsl
  • Create your own plugins!
    1. Create a ‘vendor’ directory in your app directory
    2. Lay it out like: myapp.rb : root
         |- vendor
      
                      | - plugin_name
      
                    | - init.rb  # load and hook here
      
                    | - lib
      
                          |- modules/classes here
    3. Use it in your app!

For further details visit:

Related posts:

  1. Three Ways You Can Speed Up Your Fresh Rails Development A Ruby on Rails web application I’ve been developing...

Related posts brought to you by Yet Another Related Posts Plugin.

blog comments powered by Disqus