Skip to content

Instantly share code, notes, and snippets.

require 'sinatra'
get '/' do
erb :"pages/index"
end
<!DOCTYPE html>
<html>
<head>
<title>HTML5</title>
</head>
<body>
Content here...
</body>
</html>
@andyhawthorne
andyhawthorne / sinatra_simple.rb
Created August 26, 2012 19:59
Basic Sinatra file
require 'sinatra'
get '/' do
"Hello, World!"
end
@andyhawthorne
andyhawthorne / index.erb
Created August 26, 2012 20:32
Basic erb file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
</head>
<body>
@andyhawthorne
andyhawthorne / sinatra_erb_example.rb
Created August 26, 2012 20:43
Sinatra method with instance variable
get '/' do
@time = Time.new
erb :index
end
@andyhawthorne
andyhawthorne / get_files.rb
Created August 26, 2012 22:35
method for listing files
def get_files(path)
dir_list_array = Array.new
Find.find(path) do |f|
dir_list_array << File.basename(f, ".*") if !File.directory?(f)
end
return dir_list_array
end
@andyhawthorne
andyhawthorne / index.rb
Created August 26, 2012 22:47
blog index
get '/' do
@arr = get_files('./views/posts/')
erb :index
end
@andyhawthorne
andyhawthorne / posts_list.erb
Created August 26, 2012 22:49
list of posts
<ul>
<% @arr.each do |page| %>
<li><a href="/view/<%=page%>"><%=page %></a></li>
<% end %>
</ul>
@andyhawthorne
andyhawthorne / view.rb
Created August 26, 2012 23:05
view method
get '/view/:link' do
halt 404 unless File.exist?("views/posts/#{params[:link]}.md")
markdown :"posts/#{params[:link]}", :layout_engine => :erb
end
@andyhawthorne
andyhawthorne / 404.rb
Created August 27, 2012 09:49
Sinatra 404 method
not_found do
erb :missing
end