soukouki’s diary

誰かの役に立つ記事をかけたらいいなあ

Hamlの組み込み

Hamlをテキストから直接実行したいときのメモ

require "haml"
template = <<'EOS'
%p= a1
%p= a2*3
- a3.each do |i|
  %p= i**2
EOS

haml_engine = Haml::Engine.new(template)
puts haml_engine.render(Object.new, a1:123, a2:10, a3:[1, 2, 3, 5])

ヒアドキュメントはインデントが合わなくなるから嫌いです・・・

参考

File: REFERENCE — Haml Documentation