How to specify a custom Sass directory with Sinatra

Instead of serving the Sass files from the default views directory, I would like to change this to /assets/sass

In my main ruby ​​root file in the application, the following attempts:

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 

It works! however, should there be something like the lines set :sass, Proc.new { File.join(root, "assets/sass") } that sets this for me?

+4
source share
5 answers

Currently there is no such method, since Sinatra currently accepts only one view directory.

You can try sinatra-compass and set :compass, :sass_dir => 'assets' and place only one sass file in your view folder, which will just be @import stylesheet.sass or you can 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' 
+1
source

Install the template directory, then manually draw Sass :: Engine.

 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 
+3
source

You might want to read this article. http://railscoder.com/setting-up-sinatra-to-use-slim-sass-and-coffeescript/

After I met many sites, I managed to get my sass files in a different directory instead of the "views" directory with this article.

+1
source

Currently, I cannot verify this myself, but you have tried the following.

 set :sass, File.dirname(__FILE__) + '/assets' 

EDIT : A link to Sass may also help.

0
source

This probably does not help, since I assume that you have other things in the views that you want to leave, but you can also change the views directory ...

 set :views, File.dirname(__FILE__) + '/assets' 

Then you could do:

 get '/stylesheet.css' do sass :'sass/core' end 
0
source

Source: https://habr.com/ru/post/1311151/


All Articles