# -*- coding: us-ascii -*- # frozen_string_literal: true # = ERB -- Ruby Templating # # Author:: Masatoshi SEKI # Documentation:: James Edward Gray II, Gavin Sinclair, and Simon Chiang # # See ERB for primary documentation and ERB::Util for a couple of utility # routines. # # Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI # # You can redistribute it and/or modify it under the same terms as Ruby. # A NOTE ABOUT TERMS: # # Formerly: The documentation in this file used the term _template_ to refer to an ERB object. # # Now: The documentation in this file uses the term _template_ # to refer to the string input to ERB.new. # # The reason for the change: When documenting the ERB executable erb, # we need a term that refers to its string input; # _source_ is not a good idea, because ERB#src means something entirely different; # the two different sorts of sources would bring confusion. # # Therefore we use the term _template_ to refer to: # # - The string input to ERB.new # - The string input to executable erb. # require 'erb/version' require 'erb/compiler' require 'erb/def_method' require 'erb/util' # :markup: markdown # # Class **ERB** (the name stands for **Embedded Ruby**) # is an easy-to-use, but also very powerful, [template processor][template processor]. # # ## Usage # # Before you can use \ERB, you must first require it # (examples on this page assume that this has been done): # # ``` # require 'erb' # ``` # # ## In Brief # # Here's how \ERB works: # # - You can create a *template*: a plain-text string that includes specially formatted *tags*.. # - You can create an \ERB object to store the template. # - You can call instance method ERB#result to get the *result*. # # \ERB supports tags of three kinds: # # - [Expression tags][expression tags]: # each begins with `'<%='`, ends with `'%>'`; contains a Ruby expression; # in the result, the value of the expression replaces the entire tag: # # template = 'The magic word is <%= magic_word %>.' # erb = ERB.new(template) # magic_word = 'xyzzy' # erb.result(binding) # => "The magic word is xyzzy." # # The above call to #result passes argument `binding`, # which contains the binding of variable `magic_word` to its string value `'xyzzy'`. # # The below call to #result need not pass a binding, # because its expression `Date::DAYNAMES` is globally defined. # # ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday." # # - [Execution tags][execution tags]: # each begins with `'<%'`, ends with `'%>'`; contains Ruby code to be executed: # # template = '<% File.write("t.txt", "Some stuff.") %>' # ERB.new(template).result # File.read('t.txt') # => "Some stuff." # # - [Comment tags][comment tags]: # each begins with `'<%#'`, ends with `'%>'`; contains comment text; # in the result, the entire tag is omitted. # # template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.' # ERB.new(template).result # => "Some stuff; more stuff." # # ## Some Simple Examples # # Here's a simple example of \ERB in action: # # ``` # template = 'The time is <%= Time.now %>.' # erb = ERB.new(template) # erb.result # # => "The time is 2025-09-09 10:49:26 -0500." # ``` # # Details: # # 1. A plain-text string is assigned to variable `template`. # Its embedded [expression tag][expression tags] `'<%= Time.now %>'` includes a Ruby expression, `Time.now`. # 2. The string is put into a new \ERB object, and stored in variable `erb`. # 4. Method call `erb.result` generates a string that contains the run-time value of `Time.now`, # as computed at the time of the call. # # The # \ERB object may be re-used: # # ``` # erb.result # # => "The time is 2025-09-09 10:49:33 -0500." # ``` # # Another example: # # ``` # template = 'The magic word is <%= magic_word %>.' # erb = ERB.new(template) # magic_word = 'abracadabra' # erb.result(binding) # # => "The magic word is abracadabra." # ``` # # Details: # # 1. As before, a plain-text string is assigned to variable `template`. # Its embedded [expression tag][expression tags] `'<%= magic_word %>'` has a variable *name*, `magic_word`. # 2. The string is put into a new \ERB object, and stored in variable `erb`; # note that `magic_word` need not be defined before the \ERB object is created. # 3. `magic_word = 'abracadabra'` assigns a value to variable `magic_word`. # 4. Method call `erb.result(binding)` generates a string # that contains the *value* of `magic_word`. # # As before, the \ERB object may be re-used: # # ``` # magic_word = 'xyzzy' # erb.result(binding) # # => "The magic word is xyzzy." # ``` # # ## Bindings # # A call to method #result, which produces the formatted result string, # requires a [Binding object][binding object] as its argument. # # The binding object provides the bindings for expressions in [expression tags][expression tags]. # # There are three ways to provide the required binding: # # - [Default binding][default binding]. # - [Local binding][local binding]. # - [Augmented binding][augmented binding] # # ### Default Binding # # When you pass no `binding` argument to method #result, # the method uses its default binding: the one returned by method #new_toplevel. # This binding has the bindings defined by Ruby itself, # which are those for Ruby's constants and variables. # # That binding is sufficient for an expression tag that refers only to Ruby's constants and variables; # these expression tags refer only to Ruby's global constant `RUBY_COPYRIGHT` and global variable `$0`: # # ``` # template = <