require 'erb'
weekday = Time.now.strftime('%A')
simple_template = "Today is <%= @weekday %>."
renderer = ERB.new(simple_template)
puts output = renderer.result()
结果是今天是。这不是预期的。怎么解决?
答案 0 :(得分:1)
因为您没有定义@weekday
。您在模板中使用实例变量,因此您需要定义它:
require 'erb'
@weekday = Time.now.strftime('%A')
simple_template = "Today is <%= @weekday %>."
renderer = ERB.new(simple_template)
puts output = renderer.result()
OR
您可以在模板中使用已定义的局部变量:
require 'erb'
weekday = Time.now.strftime('%A')
simple_template = "Today is <%= weekday %>."
renderer = ERB.new(simple_template)
puts output = renderer.result()