我正在寻找一种在 thors 模板操作中将选项传递给 ERB 模板引擎的方法。
我偶然发现了像这样使用 thors 模板操作的 bundler cli 源代码:
opts = {:name => name,
:constant_name => constant_name,
:constant_array => constant_array,
:author_name => author_name,
:author_email => author_email
}
template(File.join("newgem/Gemfile.tt"),
File.join(target, "Gemfile"),
opts)
但是当我在我的 thor 任务中添加这样的选项时,ERB 找不到它们,我只能使用我的 thor 类中的参数和函数来设置模板中的变量。
我不知道绑定(bind)在 ruby 中是如何工作的,也许有一种方法可以通过绑定(bind)到 ERB 来传递范围。
最佳答案
通过使用实例变量,它应该可以工作。
@name = name
template("source","target")
我的模板是这样的:
<test><%= @name %></test>
这对我有用。我没有尝试传递特定值。
关于ruby - 将选项传递给 thor 中的模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6498058/