Skip to content

Instantly share code, notes, and snippets.

@burningTyger
Created September 8, 2011 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burningTyger/1204454 to your computer and use it in GitHub Desktop.
Save burningTyger/1204454 to your computer and use it in GitHub Desktop.
useful sinatra template route snippet
# use this snippet for routes that are partly dynamic, partly static.
# For example your jquery.js is static. With this route Sinatra will check
# your public dir and if it finds it there, jquery.js will be served.
# if not, the views dir will be searched. If found it's served as compiuled coffee script
# if not you will get a 404. Seems useful to me...
# btw, namelessjohn remined me that checking for the file first is good because then you
# won't create expensive and possibly leaking symbols of files that don't exist.
# also, we set last_modified which is good for caching.
get '/assets/js/:file.js' do
halt 404 unless File.exist?("views/#{params[:file]}.coffee")
time = File.stat("views/#{params[:file]}.coffee").ctime
last_modified(time)
coffee params[:file].intern
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment