Parsing LESS options in a Sinatra app

谁说我不能喝 提交于 2019-12-30 07:31:39

问题


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.


回答1:


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



回答2:


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.




回答3:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!