Skip to content

Instantly share code, notes, and snippets.

@jharjono
Created March 7, 2011 19:12
Show Gist options
  • Save jharjono/859008 to your computer and use it in GitHub Desktop.
Save jharjono/859008 to your computer and use it in GitHub Desktop.
a setup of Sinatra using Slim for HTML, Sass for CSS, and CoffeeScript for JavaScript
#!/usr/bin/env ruby
# Libraries:::::::::::::::::::::::::::::::::::::::::::::::::::::::
require 'rubygems'
require 'sinatra/base'
require 'slim'
require 'sass'
require 'coffee-script'
# Application:::::::::::::::::::::::::::::::::::::::::::::::::::
class SassHandler < Sinatra::Base
set :views, File.dirname(__FILE__) + '/templates/sass'
get '/css/*.css' do
filename = params[:splat].first
sass filename.to_sym
end
end
class CoffeeHandler < Sinatra::Base
set :views, File.dirname(__FILE__) + '/templates/coffee'
get "/js/*.js" do
filename = params[:splat].first
coffee filename.to_sym
end
end
class MyApp < Sinatra::Base
use SassHandler
use CoffeeHandler
# Configuration:::::::::::::::::::::::::::::::::::::::::::::::
set :public, File.dirname(__FILE__) + '/public'
set :views, File.dirname(__FILE__) + '/templates'
# Route Handlers::::::::::::::::::::::::::::::::::::::::::::::
get '/' do
slim :index
end
end
if __FILE__ == $0
MyApp.run! :port => 4567
end
@jordanthornquest
Copy link

Thanks! This is killer.

@delphaber
Copy link

Thanks :)

Only a note:
:public is no longer used to avoid overloading Module#public, use :public_folder or :public_dir instead

@sighmin
Copy link

sighmin commented Jun 5, 2014

This is great! Exactly what I needed to see while playing with Sinatra

@whoward
Copy link

whoward commented Mar 10, 2015

thank you - i spent hours wrangling with AssetPipeline and Sinatra::SprocketsPipeline - this is all i needed and it works great!

@whoward
Copy link

whoward commented Mar 10, 2015

of course keep in mind your use of filename.to_sym is very insecure :) in a real production app use a whitelist before calling #to_sym

@preciousgeorge
Copy link

assest-handler
Has this worked for anybody? I have seen several tuts on how to set up assets on sinatra and non i mean non worked, i am so pissed right now, i am thinking of switching to flask. Nothing i have tried worked. Why is it so difficult????? Everybody only has this one page setup, it that all there is? I want to pack my css into its onw folder and my js too, heck i want to use foundation and nothing has work so far. Attached is my structure...
capture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment