6b3a9e760dc41f48e4a215a0aacacc02

把ERB轉成HAML

jamesy829Ruby on Rails 節點 中發起
最後由 jamesy829回應 , 463次閱讀

不知道有沒有人PO這類的Script,所以我來分享我的Script
這個Rake Task Script可以把View裡面得 .html.erb 檔案轉成.html.haml

#Include the HTML class
require 'haml/html'
namespace :hamlify do
desc "Convert ERB to Haml"
task :convert => :environment do

#Cycles through the views folder and searches for erb files
Dir["#{Rails.root}/app/views/**/*.erb"].each do |file_name|

  puts "Hamlifying: #{file_name}"

  #Creates a new file path for the haml to be exported to
  haml_file_name = file_name.gsub(/erb$/, "haml")

  #If haml is missing create it and get rid of the erb
  if !File.exist?(haml_file_name)

    #Reads erb from file
    erb_string = File.open(file_name).read

    #Converts erb to haml
    haml_string = Haml::HTML.new(erb_string, :erb => true).render

    #Writes the haml
    f = File.new(haml_file_name, "w")
    f.write(haml_string)

    #Gets rid of the erb
    File.delete(file_name)
  end
end

end
end

我是把這個Rake File 放在rails_project/lib/ 裡面。大家試試看吧!

截至 ,共收到 2 條回應
C6c57c07843274735d6f5dc451a203ee
cqpx 1樓, 於回應

http://github.com/plataformatec/devise/wiki/How-To:-Create-Haml-and-Slim-Views

$ for i in `find app/views/devise -name '*.erb'` ; do html2haml -e $i ${i%erb}haml ; rm $i ; done
6b3a9e760dc41f48e4a215a0aacacc02
jamesy829 2樓, 於回應

謝謝分享,我會去看看slim的,還不熟悉。

需要 登入 後方可回應,如果你還沒有帳號按這裡 註冊