I'm having trouble changing the path that LESS uses to include imports. My routes file has
get "/css/main.css" do
less :main, :paths => ["public/css"]
end
However, if I include an external less file with @import
in my stylesheet the LESS compiler cannot find the file. I've placed a copy in both the views
path and public/css
directories, but it still can't find it. It CAN however find plain .css files in public\css
.
I hit this today, and was able to solve it like so:
require 'less'
require 'sinatra/base'
class App < Sinatra::Base
# Make LESS @import statements work
Less.paths << settings.views
# Use LESS for CSS
get '/stylesheets/:style.css' do
less(params[:style].to_sym)
end
end
You can use something like this here which I use to pick up all .css files whether scss or plain css (i.e. it looks in public first by default and then moves on to check in views):
get '/css/:file.css' do
halt 404 unless File.exist?("views/#{params[:file]}.scss")
time = File.stat("views/#{params[:file]}.scss").ctime
last_modified(time)
scss params[:file].intern
end
You'd have to replace scss with less to make it work.
Passing options to Less should be fixed in tilt by this commit 70465f9.
If you are using bundler, adding the following line to your Gemfile
should fix the problem:
gem 'tilt', :git => 'git://github.com/rtomayko/tilt.git'
来源:https://stackoverflow.com/questions/9606703/parsing-less-options-in-a-sinatra-app