If you are using HAML and SASS in your Rails application, then any templates you define in public/stylesheet/*.sass will be compiled into *.css stylesheets. From your code, you use stylesheet_link_tag to pull in the asset by name without having to worry about the extension.
如果您在Rails应用程序中使用HAML和SASS,那么您在public / stylesheet / * .ssss中定义的任何模板都将编译为* .css样式表。从您的代码中,您可以使用stylesheet_link_tag按名称提取资产,而无需担心扩展名。
Many people dislike storing generated code or compiled code in version control, and it also stands to reason that the public/ directory shouldn't contain elements that you don't send to the browser.
许多人不喜欢在版本控制中存储生成的代码或编译的代码,并且还有理由说public / directory不应包含您不发送给浏览器的元素。
What is the best pattern to follow when laying out SASS resources in your Rails project?
在Rails项目中布置SASS资源时,最好的模式是什么?
5 个解决方案
#1
11
I always version all stylesheets in "public/stylesheets/sass/*.sass" and set up an exclude filter for compiled ones:
我总是在“public / stylesheets / sass / * .sass”中对所有样式表进行版本化,并为已编译的样式设置排除过滤器:
/public/stylesheets/*.css
#2
13
The compass framework recommends putting your sass stylesheets under app/stylesheets and your compiled css in public/stylesheets/compiled.
指南针框架建议将您的sass样式表放在app / stylesheets下,将编译后的css放在public / stylesheets / compiled中。
You can configure this by adding the following code to your environment.rb:
您可以通过将以下代码添加到environment.rb来配置它:
Sass::Plugin.options[:template_location] = {
"#{RAILS_ROOT}/app/stylesheets" => "#{RAILS_ROOT}/public/stylesheets/compiled"
}
If you use the compass framework, it sets up this configuration for you when you install it.
如果您使用指南针框架,它会在您安装时为您设置此配置。
#3
6
Honestly, I like having my compiled SASS stylesheets in version control. They're small, only change when your .sass files change, and having them deploy with the rest of your app means the SASS compiler doesn't ever need to fire in production.
老实说,我喜欢在版本控制中编译我编译的SASS样式表。它们很小,只有在.sass文件发生变化时才会更改,并且将它们与应用程序的其余部分一起部署意味着SASS编译器不需要在生产中触发。
The other advantage (albeit a small one) is that if you're not using page caching, your rails process doesn't need to have write access to your public_html
directory. So there's one fewer way an exploit of your server can be evil.
另一个优点(尽管很小)是,如果您不使用页面缓存,则rails进程不需要具有对public_html目录的写访问权限。因此,利用服务器的方式可能会少一些。
#4
5
Somewhat related, but it's a good idea to regenerate your CSS during your capistrano deployments. This callback hook does just that:
有点相关,但在capistrano部署期间重新生成CSS是个好主意。这个回调钩子就是这样做的:
after "deploy:update_code" do
rails_env = fetch(:rails_env, "production")
run "#{release_path}/script/runner -e #{rails_env} 'Sass::Plugin.update_stylesheets'"
end
Update: This should no longer be necessary with modern versions of Haml/Sass.
更新:现在版本的Haml / Sass不再需要这个。
#5
0
If I can manage it, I like to store all of my styles in SASS templates when I choose HAML/SASS for a project, and I'll remove application.css and scaffold.css. Then I will put SASS in public/stylesheets/sass, and add /public/stylesheets/*.css to .gitignore.
如果我可以管理它,我喜欢在为项目选择HAML / SASS时将所有样式存储在SASS模板中,并且我将删除application.css和scaffold.css。然后我将把SASS放在public / stylesheets / sass中,并将/public/stylesheets/*.css添加到.gitignore。
If I have to work with a combination of SASS and CSS based assets, it's a little more complicated. The simplest way of handling this is to have an output subdirectory for generated CSS within the stylesheets directory, then exclude that subdirectory in .gitignore. Then, in your views you have to know which styling type you're using (SASS or CSS) by virtue of having to select the public/stylesheets/foo stylesheet or the public/stylesheets/sass-out/foo stylesheet.
如果我必须使用基于SASS和CSS的资产组合,那就有点复杂了。处理此问题的最简单方法是在stylesheets目录中为生成的CSS创建一个输出子目录,然后在.gitignore中排除该子目录。然后,在您的视图中,您必须知道您正在使用哪种样式类型(SASS或CSS),因为必须选择public / stylesheets / foo样式表或public / stylesheets / sass-out / foo样式表。
If you have to go the second route, build a helper to abstract away the sass-out subdirectory.
如果你必须走第二条路线,建立一个帮助器来抽象出sass-out子目录。
#1
11
I always version all stylesheets in "public/stylesheets/sass/*.sass" and set up an exclude filter for compiled ones:
我总是在“public / stylesheets / sass / * .sass”中对所有样式表进行版本化,并为已编译的样式设置排除过滤器:
/public/stylesheets/*.css
#2
13
The compass framework recommends putting your sass stylesheets under app/stylesheets and your compiled css in public/stylesheets/compiled.
指南针框架建议将您的sass样式表放在app / stylesheets下,将编译后的css放在public / stylesheets / compiled中。
You can configure this by adding the following code to your environment.rb:
您可以通过将以下代码添加到environment.rb来配置它:
Sass::Plugin.options[:template_location] = {
"#{RAILS_ROOT}/app/stylesheets" => "#{RAILS_ROOT}/public/stylesheets/compiled"
}
If you use the compass framework, it sets up this configuration for you when you install it.
如果您使用指南针框架,它会在您安装时为您设置此配置。
#3
6
Honestly, I like having my compiled SASS stylesheets in version control. They're small, only change when your .sass files change, and having them deploy with the rest of your app means the SASS compiler doesn't ever need to fire in production.
老实说,我喜欢在版本控制中编译我编译的SASS样式表。它们很小,只有在.sass文件发生变化时才会更改,并且将它们与应用程序的其余部分一起部署意味着SASS编译器不需要在生产中触发。
The other advantage (albeit a small one) is that if you're not using page caching, your rails process doesn't need to have write access to your public_html
directory. So there's one fewer way an exploit of your server can be evil.
另一个优点(尽管很小)是,如果您不使用页面缓存,则rails进程不需要具有对public_html目录的写访问权限。因此,利用服务器的方式可能会少一些。
#4
5
Somewhat related, but it's a good idea to regenerate your CSS during your capistrano deployments. This callback hook does just that:
有点相关,但在capistrano部署期间重新生成CSS是个好主意。这个回调钩子就是这样做的:
after "deploy:update_code" do
rails_env = fetch(:rails_env, "production")
run "#{release_path}/script/runner -e #{rails_env} 'Sass::Plugin.update_stylesheets'"
end
Update: This should no longer be necessary with modern versions of Haml/Sass.
更新:现在版本的Haml / Sass不再需要这个。
#5
0
If I can manage it, I like to store all of my styles in SASS templates when I choose HAML/SASS for a project, and I'll remove application.css and scaffold.css. Then I will put SASS in public/stylesheets/sass, and add /public/stylesheets/*.css to .gitignore.
如果我可以管理它,我喜欢在为项目选择HAML / SASS时将所有样式存储在SASS模板中,并且我将删除application.css和scaffold.css。然后我将把SASS放在public / stylesheets / sass中,并将/public/stylesheets/*.css添加到.gitignore。
If I have to work with a combination of SASS and CSS based assets, it's a little more complicated. The simplest way of handling this is to have an output subdirectory for generated CSS within the stylesheets directory, then exclude that subdirectory in .gitignore. Then, in your views you have to know which styling type you're using (SASS or CSS) by virtue of having to select the public/stylesheets/foo stylesheet or the public/stylesheets/sass-out/foo stylesheet.
如果我必须使用基于SASS和CSS的资产组合,那就有点复杂了。处理此问题的最简单方法是在stylesheets目录中为生成的CSS创建一个输出子目录,然后在.gitignore中排除该子目录。然后,在您的视图中,您必须知道您正在使用哪种样式类型(SASS或CSS),因为必须选择public / stylesheets / foo样式表或public / stylesheets / sass-out / foo样式表。
If you have to go the second route, build a helper to abstract away the sass-out subdirectory.
如果你必须走第二条路线,建立一个帮助器来抽象出sass-out子目录。