How to specify custom Sass directory with Sinatra
Instead of serving my Sass files from the default 'views' directory I'd like to change this to /assets/sass
The following attempts are in my main ruby root file in the app:
Attempt 1:
set :sass, Proc.new { File.join(root, "assets/sass") }
get '/stylesheet.css' do
sass :core
end
With this I get the following error:
myapp.rb:17 NoMethodError: undefined method `merge' for "/Users/x/x/x/mysinatraapp/assets/sass":String
Attempt 2:
get '/stylesheet.css' do
sass :'/assets/sass/core'
end
Attempt 3:
get '/stylesheet.css' do
sass :'/assets/sass/core'
end
Both return the following error:
Errno::ENOENT: No such file or directory - ./views/assets/sass/core.sass
Attempt 4:
get '/stylesheet.css' do
sass :'../assets/sass/core'
end
This works! however, there must be something along the lines of set :sass, Proc.new { File.join(root, "assets/sass") }
that sets thi开发者_高级运维s up for me?
Set your template directory then render a Sass::Engine manually.
require 'sinatra'
require 'sass'
SASS_DIR = File.expand_path("../stylesheets", __FILE__)
get "/" do
erb :index
end
get "/stylesheets/:stylesheet.css" do |stylesheet|
content_type "text/css"
template = File.read(File.join(SASS_DIR, "#{stylesheet}.sass"))
Sass::Engine.new(template).render
end
There is no such way at the moment, as Sinatra currently only accepts a single view directory.
You could try using sinatra-compass and set :compass, :sass_dir => 'assets'
and only place a single sass file in your view folder, that will simply @import stylesheet.sass
or you could overwrite #sass
:
helpers do
def sass(template, *args)
template = :"#{settings.sass_dir}/#{template}" if template.is_a? Symbol
super(template, *args)
end
end
set :sass_dir, '../assets'
You might want to have a look at this article. http://railscoder.com/setting-up-sinatra-to-use-slim-sass-and-coffeescript/
After coming across numerous sites, I was able to achieve having my sass files in a different directory instead of the 'views' directory with this article.
I currently can't test this myself, but have you tried the following.
set :sass, File.dirname(__FILE__) + '/assets'
EDIT: The Sass reference might help as well.
This probably doesn't help since I'm guessing you have other stuff under views
that you want to stay put, but you can change the views
directory as well...
set :views, File.dirname(__FILE__) + '/assets'
Then you could do:
get '/stylesheet.css' do
sass :'sass/core'
end
精彩评论