Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@jordanthornquest jordanthornquest commented Aug 30, 2013

Thanks! This is killer.

@delphaber

This comment has been minimized.

Copy link

@delphaber delphaber commented Jan 11, 2014

Thanks :)

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

@sighmin

This comment has been minimized.

Copy link

@sighmin sighmin commented Jun 5, 2014

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

@whoward

This comment has been minimized.

Copy link

@whoward 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

This comment has been minimized.

Copy link

@whoward 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

This comment has been minimized.

Copy link

@preciousgeorge preciousgeorge commented Mar 20, 2016

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
You can’t perform that action at this time.