What is the meaning of erb?

2019-01-12 19:06发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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



回答6:

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