yield in ERB without rails

前端 未结 6 1729
轮回少年
轮回少年 2020-12-30 05:49

How can I use yield for template inheritance in erb? I want to use erb in a plain ruby CGI script and want to use a base template and subtemplate like it Rails wit

6条回答
  •  萌比男神i
    2020-12-30 06:23

    def a
      ERB.new('<%= yield %>').result(binding)
    end
    a{123}
    #=> "123"
    

    It's important that the call to Kernel#binding be inside a method, that way the context includes the block (ERB#result won't take a block).

提交回复
热议问题