2012-09-05 75 views
1

我有问题让我的萨姆工作与我的哈姆模板。Sinatra自定义SASS目录

最近,我在下面我主要sinatra.rb应用程序代码:

require 'sinatra' 
require 'dm-core' 
require 'dm-migrations' 
require 'haml' 
require 'sass' 
require 'shotgun' 


set :views, :sass => 'views/css', :haml => 'template', :default => 'views' 

helpers do 
def find_template(views, name, engine, &block) 
    _, folder = views.detect { |k,v| engine == Tilt[k] } 
    folder ||= views[:default] 
    super(folder, name, engine, &block) 
end 
end 


get '/css/styles.css' do 
sass :styles 
end 


get '/' do 
haml :index 
end 


I have following application directory structure: 
site 
|site.rb 
|-sass > styles.scss (my scss file generate css realtime using sass --watch sass:css command         
|-css > styles.css 
|-template > index.haml 

我index.haml文件,该文件是在模板文件夹中呈现的罚款。

我index.haml模板:

!!! XML 
!!! 
%html 
%head 
    %title Some title 
    %meta{"http-equiv" => "Content-Type", :content => "text/html; charset=utf-8"} 
    %link{"rel" => "stylesheet", "href" => "views/css/styles.css", "type" => "text/css"} 
%body 
    %h1 Some h1 
    %p Some p 

回答

2

你已经给出了你的视图文件的路径,而不是路径来显示SASS文件的路径。

%link{"rel" => "stylesheet", "href" => "/css/styles.css", "type" => "text/css"} 
2

您需要设置SASS views目录,以正确的。

set :views, :sass => 'views/sass', :haml => 'template', :default => 'views' 

如果您希望sass返回呈现的styles.css,那么它需要从sass文件所在的位置开始。如果你想让它返回真正的.css输出,那么不要让sass在你的路由中渲染它,只要返回文件。

+0

我已经把我的工作目录到一个你建议我。另一个问题是我应该把我的.css/.scss文件放在哪里?进入我的应用程序根目录? Atm im我的Shothun服务器产生以下错误: Errno :: ENOENT - 没有这样的文件或目录 - views/css/styles.sass: –

+0

您应该将.scss文件放在您设置sass views目录的任何位置因为那是它寻找那些观点的地方。 –

0

我找到了解决方案。

require 'sinatra' 
require 'dm-core' 
require 'dm-migrations' 
require 'haml' 
require 'sass' 
require 'shotgun' 


set :views, :scss => 'views/', :haml => 'template', :default => 'views' 

helpers do 
def find_template(views, name, engine, &block) 
    _, folder = views.detect { |k,v| engine == Tilt[k] } 
    folder ||= views[:default] 
    super(folder, name, engine, &block) 
end 
end 


get '/css/:name.css' do 
    scss :styles 
end 


get '/' do 
    haml :index 
end 

因此,大家可以看到,而不是:

get '/css/styles.css' do 
    sass :styles 
end 

它应该是:

get '/css/:name.css' do 
    scss :styles 
end 

然后我把我的styles.scss到我的/ views文件夹。您.scss文件SCSS =>“路径:但是,您可以通过编辑修改目的地目录

set :views, :scss => 'views/', :haml => 'template', :default => 'views'