erb 是什么意思?

发布于 2024-10-04 17:05:55 字数 66 浏览 5 评论 0原文

为什么 Rails 应用程序的视图采用 *.erb.html 格式? “erb”是什么意思?

Why is the view of Rails application in the format *.erb.html? What does "erb" mean?

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

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

发布评论

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

评论(6

月依秋水 2024-10-11 17:05:55

erb 代表“嵌入式 Ruby”。 .html.erb.erb.html 文件是嵌入了 Ruby 代码的 HTML; Rails 将评估 Ruby 以动态地将内容添加到文件中,并输出一个“纯”HTML 文件进行渲染。

erb stands for "Embedded RuBy". A .html.erb or .erb.html file 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.

原野 2024-10-11 17:05:55

正如 @Chowlett 之前提到的,erb 代表嵌入式 Ruby。当您将任何文件定义为“.html.erb”时,这意味着它是一个嵌入了 ruby​​ 代码的 HTML 文件,它类似于 Rails 文件的“.rhtml”扩展名。

您可以看到“.html.erb”和“.rhtml”之间的详细且良好的区别 点击这里

与“.rhtml”相同,也可以将“.rjs”扩展名重命名为“.js.erb”或“.rxml”重命名为“.xml.erb”

这种格式将内容类型与模板引擎分开,在本例中为“erb”。

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.

You can see a detailed and nice difference between ".html.erb" and ".rhtml" Click Here

Same as ".rhtml", you can also rename ".rjs" extension to ".js.erb" or ".rxml" to ".xml.erb"

This format separates out content type from template engine which is "erb" in this case.

牵你手 2024-10-11 17:05:55

来自 Stuart Ellis 的ERB 模板简介

ERB(嵌入式 Ruby)是 Ruby 的一项功能,使您能够方便地从模板生成任意数量的任意类型的文本。模板本身将纯文本与 Ruby 代码结合起来,用于变量替换和流程控制,这使得它们易于编写和维护。

虽然 ERB 最常见的是生成网页,但它也用于生成 XML 文档、RSS 提要、源代码和其他形式的结构化文本文件。当您需要创建包含许多标准模式重复项(例如单元测试套件)的文件时,它可能非常有价值。

ERB 的主要组件是一个库,您可以在 Ruby 应用程序和 Rake 任务中调用它。该库接受任何字符串作为模板,并且对模板的来源没有限制。您可以完全在代码中定义模板,或将其存储在外部位置并根据需要加载。这意味着您可以将模板保存在文件、SQL 数据库或您想要使用的任何其他类型的存储中。

Ruby 发行版还包含一个命令行实用程序,使您能够处理文件中保存的模板,而无需编写任何其他代码。从逻辑上讲,这个实用程序称为 erb。

ERB 是 Ruby 标准库的一部分。您无需安装任何其他软件即可使用它。

原始文章包含更多详细信息和使用 ERB 的简短指南。您还可以阅读官方文档


注意:上面引用的块之前是其他用户作为答案发布的,没有链接到ERB 模板简介或承认这不是该用户的工作。该帖子因抄袭而被(正确地)删除。不过,我认为这是一个有用的答案,因此我重新发布了该引用,并正确注明了原作者 Stuart Ellis 的归属。

From Stuart Ellis's An Introduction to ERB Templating:

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.

The original article contains more detail and a short guide to using ERB. You can also read the official docs.


Note: the quoted block above was previously posted as an answer by another user without linking to An Introduction to ERB Templating or acknowledging that it was not that user's work. That post was (rightly) deleted for plagiarism. However, I thought it was a useful answer, so I've reposted the quote giving proper attribution to Stuart Ellis, the original author.

神仙妹妹 2024-10-11 17:05:55

嵌入式 Ruby,也称为 ERb,是在网页中包含动态内容的主要模板系统。 ——迈克尔·赫特尔

Embedded Ruby, also called ERb, is the primary template system for including dynamic content in web pages. --Michael Hertl

夜未央樱花落 2024-10-11 17:05:55

来自 模板格式
包含 ERB 模板的文件可以具有任意名称,但按照惯例,文件名应以 .erb 扩展名结尾。 Rails 要求模板文件具有输出类型的扩展名,后跟 .erb,因此像layout.html.erb 这样的名称表示 HTML 模板。

erb 文件只会输出文本。而已。什么文本取决于文件内静态文本和 ruby​​ 代码的混合。您可以使用 erb 生成 html,这是 Rails 中的默认用法,因为这就是浏览器显示页面所需的内容。

在 ruby​​ 中,有一些默认设置,其中一个默认设置是控制器将呈现 html 页面。但是,如果您为其编写一个 Web api,则可以轻松地使其使用 .xml 或 .json 或 .csv 进行响应。

Erb 是一个生成文本的库类。而已。它需要一个包含静态文本和混合 ruby​​ 代码的文件。它将运行 ruby​​ 代码并将结果写入另一个文件,如果您的控制器是 html

from template-format
A 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.

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.

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

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

最美的太阳 2024-10-11 17:05:55

ERB 是 Ruby 中的模板类,通常用在 Rails 中的 .rhtml 或 .erb.html(嵌入 Ruby 的 HTML)中。

这是 Ruby 文档的详细信息。

http://ruby-doc.org/stdlib-1.9 .3/libdoc/erb/rdoc/ERB.html

ERB is templating Class in Ruby and is often used in .rhtml or .erb.html (HTML with embedded Ruby) in rails.

Here is a nice detail on the Ruby docs.

http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html

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