ruby - 将选项传递给 thor 中的模板函数

标签 ruby templates erb thor

我正在寻找一种在 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/

相关文章:

c++ - 使用模板的类型转换有奇怪的行为

C++ 模板类型名迭代器

ruby - 循环遍历yaml集合,按key排序

ruby - 如何将这个 Objective C 重写为 RubyMotion?

ruby - "+="(加上等于)是什么意思?

ruby-on-rails - 如何确保同一个作业不会同时运行两次?

ruby - Erb View 中不需要的空格

ruby - 如何在中断前执行方法

c++ - 模板 + friend (致命组合)

javascript - 如何强制 js.erb 刷新 Ruby 代码