生成器和模板有什么区别?

发布于 2024-11-07 17:34:38 字数 478 浏览 2 评论 0原文

在 Rails 中,我们有生成器和模板来快速设置和配置应用程序。从表面上看,生成器和模板之间似乎有两个区别:

  1. 您可以在使用 rails new appname -m path/to/template.rb 创建新应用程序时应用模板。生成器必须在使用 rails g Generator_name 创建 Rails 应用程序后运行
  2. 生成器是由一系列公共方法组成的类,这些方法按照声明的顺序触发。模板只是一组命令。

在我看来,这似乎使生成器和模板本质上相同。如果我必须选择,我会选择模板,因为按照声明的顺序触发一系列公共方法对我来说似乎是一个相当奇怪的 OO 抽象。一系列简单的命令更加清晰,并且不需要读者了解相当不寻常的约定来理解模板。

生成器和模板之间还有什么更显着的区别吗?如果是这样,是否有一种情况比另一种更适合?如果不是,为什么 Rails 两者都有,并且其中一种方法可能会在某个时候被弃用?

In Rails we have both generators and templates to quickly setup and configure applications. Superficially, there seems to be 2 differences between generators and templates:

  1. You can apply templates when creating a new application with rails new appname -m path/to/template.rb. Generators must be run after the creation of a rails app with rails g generator_name
  2. Generators are classes made up of a series of public methods that are fired in the sequence they are declared. Templates are just a set of commands.

To my mind, this seems to make both generators and templates essentially identical. If I had to choose, I would go with templates because firing a series of public methods in the order they are declared seems to be quite a weird OO abstraction to me. A series of simple commands is far clearer and doesn't require the reader to know that rather unusual convention in order to understand templates.

Are there any more significant differences between generators and templates? If so, are there situations where one is more suitable than another? If not, why does Rails have both and is one method likely to become deprecated at some point?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-11-14 17:34:38

我认为您所描述的主要是先有鸡还是先有蛋的问题 - 您所描述的模板(不要与操作视图的模板混淆)必须在创建应用程序之前应用,而生成器确实在 Rails 应用程序内工作(可以通过插件等添加更多)。苹果和橙子。

I think it's mainly a chicken and egg thing that your are describing - the template you are describing (not to be confused with the templates of action view) has to be applied before the app is created, whereas generators do work from within the rails app (and more can be added via plugins etc). Apples and Oranges.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文