Ruby templates: How to pass variables into inlined ERB?

前端 未结 10 1734
陌清茗
陌清茗 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:53

    Simple solution using Binding:

    b = binding
    b.local_variable_set(:a, 'a')
    b.local_variable_set(:b, 'b')
    ERB.new(template).result(b)
    

提交回复
热议问题