ERuby
≈ 7 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Embedded Ruby (abbreviated as ERB or eRuby) is a template engine that embeds Ruby code into text documents. It is commonly used to generate dynamic content in HTML documents, which is a role similar to Active Server Pages, JavaServer Pages, PHP, and other server-side scripting languages. eRuby combines Ruby code and plain text to provide flow control and variable substitution, thus making the combined code easier to maintain. In Ruby on Rails, eRuby is commonly used in a view layer in order to generate dynamic web content. The view module of Ruby on Rails is responsible for displaying a response or output in a browser. In its simplest form, a view may consist of static HTML content; however, many Ruby on Rails applications will require dynamic content created by the controller (an action method) to be displayed in their view. This is achieved from Embedded Ruby templates, which allow Ruby code to be embedded within view documents. During rendering, the embedded code is evaluated and replaced with its resulting value at runtime. However, the ability to embed code in a view document risks bridging the clear separation present in the MVC frame. As a result, maintaining clear separation of responsibility among the model, view, and controller modules is left to the developer of the application.
Embedded Ruby (abbreviated as ERB or eRuby) is a template engine that embeds Ruby code into text documents. It is commonly used to generate dynamic content in HTML documents, which is a role similar to Active Server Pages, JavaServer Pages, PHP, and other server-side scripting languages. eRuby combines Ruby code and plain text to provide flow control and variable substitution, thus making the combined code easier to maintain. In Ruby on Rails, eRuby is commonly used in a view layer in order to generate dynamic web content. The view module of Ruby on Rails is responsible for displaying a response or output in a browser. In its simplest form, a view may consist of static HTML content; however, many Ruby on Rails applications will require dynamic content created by the controller (an action method) to be displayed in their view. This is achieved from Embedded Ruby templates, which allow Ruby code to be embedded within view documents. During rendering, the embedded code is evaluated and replaced with its resulting value at runtime. However, the ability to embed code in a view document risks bridging the clear separation present in the MVC frame. As a result, maintaining clear separation of responsibility among the model, view, and controller modules is left to the developer of the application. The delimiter indicates that the tag encloses an expression. Such tag starts with an opening tag delimiter followed by an equal to symbol, and ends with an end tag delimiter. During the rendering of the template, this piece of code gets substituted with the result of the code. If the evaluated result is not a string, it gets converted to a string before it is rendered. require 'erb' x = 500 template = ERB.new("The value of x is: ") puts template.result(binding) Execution tags are defined by and code enclosed in such tags is called a scriptlet. The code in the respective tag gets executed, and its result gets replaced in place of the scriptlet. Such tags require a matching tag to denote the end of a functional block. The following ERB template generates an HTML list item four times: list item The following is the resulting output: list item list item list item list item As shown from the output, the text list item is printed four times. The scriptlet produces no text on its own; it only makes the enclosed statement to run multiple times. Comments are defined with the tag and do not get rendered in the output. Such tags start with an open tag delimiter followed by a hash symbol and end with an end tag delimiter. The following is an example of a comment tag: Ruby uses the same comment tags as eRuby, and all code after # is ignored, thereby generating nothing. require 'erb' x = 400 simple_template = "Value of x is: is ." renderer = ERB.new(simple_template) puts output = renderer.result(binding) This approach differs from string interpolation, which produces the output immediately when the string is constructed: x = 400 string = "The value of x is: #{x}" puts string However, constructing the string before the first variable is defined prevents the code from executing: string = "The value of x is: #{x}" x = 400 puts string Thus, the primary reason for using an ERB object is to write templates ahead of time, by binding variables and methods that may not exist at the given time. The template gets processed only when result is called on the ERB object. In order to get access to instance methods and instance variable of an object, ERB makes use of a binding object. Access to variables and methods of an object is given by the private binding object which exists in each Ruby class. It is easy to get access to methods and variables within the method of a class; however, to access variables of a different class, the class will have to expose its binding object via a public method. An example is as follows: class ERBExample attr_accessor:variable1 # using bind to access class variables def render() renderer.result(binding) end def initialize(variable1) @variable1 = variable1 end # Expose private binding() method. def get_binding binding() end end example = ERBExample.new(variable1) renderer = ERB.new(template) puts output = renderer.result(example.get_binding) As shown, the binding object of the class ERBExample is exposed. Furthermore, the binding object is used to access the variables and methods of the class within one of its methods. renderer = ERB.new(template, 3, '>') To only provide the third parameter and ignore the second parameter, use 0 as the input for second parameter. ERB has many other methods exposed which can be used to render a template. A full list of APIs exposed by the ERB object is listed in the ERB documentation.
Running ERB from Command-line
The erb command-line provides a way to process ERB templates from the terminal.It reads a template file, evaluates embedded Ruby code, and prints the result in the output. Output redirection can be used to write the result to a file rather than printing it in standard output. erb sample1.erb.txt > my_view.html.erb In the above example, output gets redirected to my_view.html.erb file. Additional third-party libraries can be loaded using the -r option followed by the name of the library, which is similar to the keyword require . The following example uses the IPAddr library: erb -r IPAddr sample1.txt.erb > my_view.html.erb For specifying safety levels, a number can be passed as a command-line argument using the -S option: erb -S 4 sample1.erb.txt > my_view.html.erb
Text derived from https://en.wikipedia.org/w/index.php?oldid=1358638110 under CC BY-SA 4.0; modifications have been made.