Created
September 8, 2011 19:38
-
-
Save burningTyger/1204454 to your computer and use it in GitHub Desktop.
useful sinatra template route snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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