我正在寻找一种方法来传递选项,以ERB模板引擎在模板的行动。
我偶然发现了bundler cli源代码,其中thors模板操作的用法如下:
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类中的参数和函数来设置模板中的变量。
我不知道绑定在ruby中是如何工作的,也许有一种方法可以通过绑定传递一个作用域到ERB。
发布于 2011-09-09 20:36:02
通过使用实例变量,它应该可以工作。
@name = name
template("source","target")我的模板如下所示:
<test><%= @name %></test>这对我很有效。我还没有尝试传递特定的值。
发布于 2011-09-30 04:35:43
我找不到任何文档来回答这个问题,但是通读Bundler CLI的源代码,似乎如果您试图在模板中引用:author_email参数,
Author email: <%= config[:author_email] %>很管用。
https://stackoverflow.com/questions/6498058
复制相似问题