1

sassをhamlテンプレートで動作させるのに問題があります。

最近、メインの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
4

3 に答える 3

2

sass ビュー ディレクトリを正しいディレクトリに設定する必要があります。

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

sass がレンダリングされた styles.css を返すようにするには、sass ファイルがある場所から開始する必要があります。実際の .css 出力を返したい場合は、ルートで sass をレンダリングせずに、ファイルを返すだけです。

于 2012-09-05T17:45:12.807 に答える
2

SASS ファイルをレンダリングするルートへのパスではなく、ビュー ファイルへのパスを指定しました。

%link{"rel" => "stylesheet", "href" => "/css/styles.css", "type" => "text/css"}
于 2012-09-05T17:47:43.873 に答える
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'
于 2012-09-06T05:12:12.507 に答える