Đây là một mẫu cơ bản cho các ứng dụng Sinatra mà tôi sử dụng. . ứng dụng như thế này bằng cách sử dụng:
thin -R config.ru start
Chỉnh sửa : Bây giờ tôi đang duy trì bộ xương Monk của riêng mình dựa trên Riblits dưới đây . Để sử dụng nó để sao chép mẫu của tôi làm cơ sở cho các dự án của riêng bạn:
# Before creating your project
monk add riblits git://github.com/Phrogz/riblits.git
# Inside your empty project directory
monk init -s riblits
Bố cục tệp:
cấu hình
ứng dụng
người giúp việc /
  init.rb
  partials.rb
mô hình /
  init.rb
  người dùng
tuyến đường /
  init.rb
  đăng nhập.rb
  chính.rb
lượt xem/
  bố trí.haml
  đăng nhập.haml
  chính.haml
 
cấu hình
root = ::File.dirname(__FILE__)
require ::File.join( root, 'app' )
run MyApp.new
 
ứng dụng
# encoding: utf-8
require 'sinatra'
require 'haml'
class MyApp < Sinatra::Application
  enable :sessions
  configure :production do
    set :haml, { :ugly=>true }
    set :clean_trace, true
  end
  configure :development do
    # ...
  end
  helpers do
    include Rack::Utils
    alias_method :h, :escape_html
  end
end
require_relative 'models/init'
require_relative 'helpers/init'
require_relative 'routes/init'
 
người trợ giúp / init.rb
# encoding: utf-8
require_relative 'partials'
MyApp.helpers PartialPartials
require_relative 'nicebytes'
MyApp.helpers NiceBytes
 
người trợ giúp / partials.rb
# encoding: utf-8
module PartialPartials
  def spoof_request(uri,env_modifications={})
    call(env.merge("PATH_INFO" => uri).merge(env_modifications)).last.join
  end
  def partial( page, variables={} )
    haml page, {layout:false}, variables
  end
end
 
người trợ giúp / Nicebytes.rb
# encoding: utf-8
module NiceBytes
  K = 2.0**10
  M = 2.0**20
  G = 2.0**30
  T = 2.0**40
  def nice_bytes( bytes, max_digits=3 )
    value, suffix, precision = case bytes
      when 0...K
        [ bytes, 'B', 0 ]
      else
        value, suffix = case bytes
          when K...M then [ bytes / K, 'kiB' ]
          when M...G then [ bytes / M, 'MiB' ]
          when G...T then [ bytes / G, 'GiB' ]
          else            [ bytes / T, 'TiB' ]
        end
        used_digits = case value
          when   0...10   then 1
          when  10...100  then 2
          when 100...1000 then 3
          else 4
        end
        leftover_digits = max_digits - used_digits
        [ value, suffix, leftover_digits > 0 ? leftover_digits : 0 ]
    end
    "%.#{precision}f#{suffix}" % value
  end
  module_function :nice_bytes  # Allow NiceBytes.nice_bytes outside of Sinatra
end
 
mô hình / init.rb
# encoding: utf-8
require 'sequel'
DB = Sequel.postgres 'dbname', user:'bduser', password:'dbpass', host:'localhost'
DB << "SET CLIENT_ENCODING TO 'UTF8';"
require_relative 'users'
 
mô hình / user.rb
# encoding: utf-8
class User < Sequel::Model
  # ...
end
 
tuyến đường / init.rb
# encoding: utf-8
require_relative 'login'
require_relative 'main'
 
tuyến đường / login.rb
# encoding: utf-8
class MyApp < Sinatra::Application
  get "/login" do
    @title  = "Login"
    haml :login
  end
  post "/login" do
    # Define your own check_login
    if user = check_login
      session[ :user ] = user.pk
      redirect '/'
    else
      redirect '/login'
    end
  end
  get "/logout" do
    session[:user] = session[:pass] = nil
    redirect '/'
  end
end
 
tuyến đường / main.rb
# encoding: utf-8
class MyApp < Sinatra::Application
  get "/" do
    @title = "Welcome to MyApp"        
    haml :main
  end
end
 
lượt xem / layout.haml
!!! XML
!!! 1.1
%html(xmlns="http://www.w3.org/1999/xhtml")
  %head
    %title= @title
    %link(rel="icon" type="image/png" href="/favicon.png")
    %meta(http-equiv="X-UA-Compatible" content="IE=8")
    %meta(http-equiv="Content-Script-Type" content="text/javascript" )
    %meta(http-equiv="Content-Style-Type" content="text/css" )
    %meta(http-equiv="Content-Type" content="text/html; charset=utf-8" )
    %meta(http-equiv="expires" content="0" )
    %meta(name="author" content="MeWho")
  %body{id:@action}
    %h1= @title
    #content= yield
               
              
require "sequel"vàDBkhởi tạomodels/init.rb, và sử dụngrequire_relativecho tất cả các tệp, đó là bạn có thể cd vàomodelsthư mục của mình , mở bảng điều khiển IRB và nhậprequire './init'và bạn đã tải cơ sở dữ liệu và mô hình đầy đủ để khám phá tương tác .