fatnic
fatnic

Reputation: 509

Parsing LESS options in a Sinatra app

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.

Upvotes: 2

Views: 1863

Answers (3)

stijnvn
stijnvn

Reputation: 366

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'

Upvotes: 0

torrancew
torrancew

Reputation: 31

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

Upvotes: 1

three
three

Reputation: 8478

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.

Upvotes: 0

Related Questions