小编dev*_*boy的帖子

将选项传递给模板函数

我正在寻找一种方法来在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)
Run Code Online (Sandbox Code Playgroud)

但是当我在我的Thor任务中添加这样的选项时,ERB找不到它们,我只能在我的thor类中使用参数和函数来在模板中设置变量.

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

ruby templates erb thor

9
推荐指数
2
解决办法
2972
查看次数

标签 统计

erb ×1

ruby ×1

templates ×1

thor ×1