繁体   English   中英

如何在Rails应用程序上的HTML模板中使用“ SASS”?

[英]How to use 'SASS' from html template on a rails app?

我正在尝试在Rails应用程序中安装html模板,因此我将所有资产移到了公共文件夹中,并重命名了erb中的所有html文件。

一切正常,除了'sass'不起作用,html模板的SASS文件夹中的任何文件都不起作用。

那么,如何在Rails应用程序中使用html模板的SASS文件?

对于引导模板,我也遇到了同样的问题。 因此,我分享我的经验,可能会对您有所帮助。 我所做的是我删除了这个宝石(如果您正在使用它)

gem 'bootstrap-sass'

并替换为这两个宝石,

gem 'therubyracer'
gem 'less-rails-bootstrap'

您还需要根据官方文档application.jsapplication.css中对其进行初始化。

看看这个博客 这是有关如何在Rails中集成自定义引导模板的分步指南。 它可能会帮助您。

那么,如何在Rails应用程序中使用html模板的SASS文件?

1)如果使用bundle install或通过命令行,请在Gemfile中安装SASS Gem gem'sass gem 'sass'gem install sass

2)在您的SASS文件夹中创建一个名为“ manifest.txt”的文件,例如public/src/sass/manifest.txt

3)在manifest.txt中,按照要合并到单个CSS文件中的包含顺序列出每个文件,例如:

framework.sass
framework_overides.sass
homepage.sass
contact.sass
global.sass

4)打开您的Rakefile并创建此rake任务:

require 'sass'
desc "Parse SASS manifest.txt and convert to single CSS file"
task :sass do
  # your template directory
  src_dir = File.join Rails.root, 'public/src'

  # SASS directory
  sass_dir = File.join src_dir, "sass"

  # name of the single output file
  app_css_file = File.join src_dir, "css/application.css"

  # parse SASS manifest
  sass_manifest = IO.readlines File.join(sass_dir, "manifest.txt")

  # clear previous app_css_file, without having to require 'fileutils'
  File.open(app_css_file, 'w') {|f| f.write ""}

  # write to app_css_file
  File.open(app_css_file, 'a+') do |f|
    sass_manifest.each do |x|

      # remove code comments
      next if x.strip =~ /^#/

      # parse extension
      case syntax = File.extname(x.strip).delete('.').downcase

      # convert and append scss
      when /scss/ then
        f.write Sass::Engine.new(IO.read(File.join(sass_dir, x.strip)), :syntax => :scss, :cache => false).render

      # convert and append sass
      when /sass/ then
        f.write Sass::Engine.new(IO.read(File.join(sass_dir, x.strip)), :cache => false).render
      end
    end
  end
end

5)使用此命令: rake sass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM