不知道有沒有人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 條回應
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
jamesy829
2樓, 於回應
謝謝分享,我會去看看slim的,還不熟悉。