Ruby-on-rails erb是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4284421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of erb?
提问by khanh
Why is the view of Rails application in the format *.erb.html? What does "erb" mean?
为什么 Rails 应用程序的视图是这种格式的*.erb.html?“erb”是什么意思?
回答by Chowlett
erb stands for "Embedded RuBy". A .html.erbor .erb.htmlfile is HTML with Ruby code embedded in; Rails will evaluate the Ruby to add content to the file dynamically, and will output a "pure" HTML file for rendering.
erb 代表“嵌入式 RuBy”。甲.html.erb或.erb.html文件是HTML与Ruby代码嵌入; Rails 将评估 Ruby 以动态地向文件添加内容,并将输出一个“纯”HTML 文件进行渲染。
回答by Nimesh Nikum
As @Chowlett mentioned before, erb stands for Embedded Ruby. When you define any file as ".html.erb" that means it's an HTML file with ruby code embedded in it and it is similar to ".rhtml" extension of rails file.
正如@Chowlett 之前提到的, erb 代表Embedded Ruby。当您将任何文件定义为“.html.erb”时,这意味着它是一个嵌入了 ruby 代码的 HTML 文件,它类似于 rails 文件的“.rhtml”扩展名。
You can see a detailed and nice difference between ".html.erb" and ".rhtml" Click Here
您可以看到“.html.erb”和“.rhtml”之间的详细而漂亮的区别点击这里
Same as ".rhtml", you can also rename ".rjs" extension to ".js.erb" or ".rxml" to ".xml.erb"
与“.rhtml”相同,您也可以将“.rjs”扩展名重命名为“.js.erb”或“.rxml”为“.xml.erb”
This format separates out content type from template engine which is "erb" in this case.
这种格式从模板引擎中分离出内容类型,在这种情况下是“erb”。
回答by Mark Amery
From Stuart Ellis's An Introduction to ERB Templating:
来自 Stuart Ellis 的ERB 模板简介:
ERB (Embedded RuBy) is a feature of Ruby that enables you to conveniently generate any kind of text, in any quantity, from templates. The templates themselves combine plain text with Ruby code for variable substitution and flow control, which makes them easy to write and maintain.
Although ERB is most commonly seen generating Web pages, it is also used to produce XML documents, RSS feeds, source code, and other forms of structured text file. It can be extremely valuable when you need to create files which include many repetitions of a standard pattern, such as unit test suites.
The main component of ERB is a library which you can call within your Ruby applications and Rake tasks. This library accepts any string as a template, and imposes no limitations on the source of the template. You may define a template entirely within your code, or store it in an external location and load it as required. This means that you can keep templates in files, SQL databases, or any other kind of storage that you want to use.
Ruby distributions also include a command-line utility that enables you to process templates that are held in files without writing any additional code. Logically, this utility is called erb.
ERB is part of the Ruby standard library. You do not need to install any other software to use it.
ERB (Embedded RuBy) 是 Ruby 的一个特性,它使您能够方便地从模板中生成任意数量的任意类型的文本。模板本身将纯文本与 Ruby 代码结合起来进行变量替换和流程控制,这使得它们易于编写和维护。
尽管 ERB 最常用于生成 Web 页面,但它也用于生成 XML 文档、RSS 提要、源代码和其他形式的结构化文本文件。当您需要创建包含许多标准模式重复的文件时,它可能非常有价值,例如单元测试套件。
ERB 的主要组件是一个库,您可以在您的 Ruby 应用程序和 Rake 任务中调用它。这个库接受任何字符串作为模板,并且对模板的来源没有限制。您可以完全在代码中定义模板,或将其存储在外部位置并根据需要加载。这意味着您可以将模板保存在文件、SQL 数据库或您想要使用的任何其他类型的存储中。
Ruby 发行版还包括一个命令行实用程序,使您能够处理保存在文件中的模板,而无需编写任何额外的代码。从逻辑上讲,此实用程序称为 erb。
ERB 是 Ruby 标准库的一部分。您无需安装任何其他软件即可使用它。
The original article contains more detail and a short guide to using ERB. You can also read the official docs.
原始文章包含更多详细信息和使用 ERB 的简短指南。您还可以阅读官方文档。
Note: the quoted block above was previously posted as an answer by another user without linking to An Introduction to ERB Templatingor acknowledging that it was not that user's work. That post was (rightly) deleted for plagiarism. However, I thought it was a usefulanswer, so I've reposted the quote giving proper attribution to Stuart Ellis, the original author.
注意:上面引用的块之前是由另一个用户作为答案发布的,没有链接到 ERB 模板简介或承认这不是该用户的工作。该帖子因抄袭(正确)被删除。但是,我认为这是一个有用的答案,因此我重新发布了该引文,并正确归因于原作者 Stuart Ellis。
回答by Andy Young
Embedded Ruby, also called ERb, is the primary template system for including dynamic content in web pages. --Michael Hertl
嵌入式 Ruby,也称为 ERb,是用于在网页中包含动态内容的主要模板系统。——迈克尔·赫特尔
回答by Ahmed Ali
from template-formatA file that contains an ERB template may have any name, but it is the convention that the name of file should end with the .erb extension. Rails requires template files to have the extension of the output type, followed by .erb, so that a name like layout.html.erb indicates a HTML template.
from template-format包含 ERB 模板的文件可以有任何名称,但约定文件名应以 .erb 扩展名结尾。Rails 要求模板文件具有输出类型的扩展名,后跟 .erb,以便像 layout.html.erb 这样的名称表示 HTML 模板。
erb files will simply output text. Nothing more. What text is depending on the mix of static text and ruby code inside the file. You can use erb to generate html which default usage in Rails, because that's what browsers need to display a page.
erb 文件将简单地输出文本。而已。什么文本取决于文件中静态文本和 ruby 代码的混合。您可以使用 erb 生成 html,这是 Rails 中的默认用法,因为这是浏览器显示页面所需要的。
In ruby there are certain defaults One default is that a controller will render a html page. But you can easily make it respond with .xml or .json or .csv if you write a web api for it
在 ruby 中有一些默认值 一种默认值是控制器将呈现 html 页面。但是如果你为它编写了一个 web api,你可以很容易地让它响应 .xml 或 .json 或 .csv
Erb is a library class that generates text. Nothing more. It expects a file that contains static text and ruby code mixed. It will run the ruby code and write the result to another file which in case for your controllers is html
Erb 是一个生成文本的库类。而已。它需要一个包含静态文本和 ruby 代码混合的文件。它将运行 ruby 代码并将结果写入另一个文件,以防您的控制器是 html
回答by hindenbug
ERB is templating Class in Ruby and is often used in .rhtml or .erb.html (HTML with embedded Ruby) in rails.
ERB 是 Ruby 中的模板类,通常用于 Rails 中的 .rhtml 或 .erb.html(嵌入 Ruby 的 HTML)。
Here is a nice detail on the Ruby docs.
这是关于 Ruby 文档的一个很好的细节。

