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

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:

No related posts.

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

  • http://afreshcup.com/?p=526 A Fresh Cup » Blog Archive » Double Shot #86

    [...] Sinatra: Classy web-development dressed in a DSL – Yet another quick way to bang together those web applications. [...]

  • http://www.builderhomeinsider.info/home-builder-custom/index.html Home Builder Custom

    Controversy Continues Between Home Builders Association and Orlando Schools…

    Since the year 2000 when the Martinez Doctrine was promoted by the Orange County School District, of which Orlando schools is a part, and adopted by Orange County as a planning tool, problems have been building between the schools and the developers ac…

  • http://www.internetwithoutphone.com/home-telephone-service/ internet without phone

    internet without phone…

    [...]Sinatra: Classy web-development dressed in a DSL | AdaRuby[...]…