Ruby templates: How to pass variables into inlined ERB?

前端 未结 10 1713
陌清茗
陌清茗 2020-11-30 21:18

I have an ERB template inlined into Ruby code:

require \'erb\'

DATA = {
    :a => \"HELLO\",
    :b => \"WORLD\",
}

template = ERB.new <<-EOF
          


        
10条回答
  •  执笔经年
    2020-11-30 21:43

    Maybe the cleanest solution would be to pass specific current local variable to erb template instead of passing the entire binding. It's possible with ERB#result_with_hash method (introduced in Ruby 2.5)

    DATA.keys.each do |current|
      result = template.result_with_hash(current: current)
    ...
    

提交回复
热议问题