11

我正在寻找一种在 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 类中使用参数和函数来设置模板中的变量。

我不知道绑定在 ruby​​ 中是如何工作的,也许有一种方法可以通过绑定到 ERB 来传递范围。

4

2 回答 2

14

通过使用实例变量,它应该可以工作。

@name = name
template("source","target")

我的模板如下所示:

<test><%= @name %></test>

这对我有用。我还没有尝试过传递特定值。

于 2011-09-09T12:36:01.713 回答
12

我找不到任何文档来回答这个问题,但是通过 Bundler CLI 的源代码阅读,如果您试图在模板中引用 :author_email 参数,

Author email: <%= config[:author_email] %>

作品。

于 2011-09-29T20:35:42.800 回答