Current File : //proc/thread-self/root/opt/alt/ruby18/share/ri/1.8/system/ERB/cdesc-ERB.yaml
--- !ruby/object:RI::ClassDescription 
attributes: 
- !ruby/object:RI::Attribute 
  comment: 
  - !ruby/struct:SM::Flow::P 
    body: The optional <em>filename</em> argument passed to Kernel#eval when the ERB code is run
  name: filename
  rw: RW
- !ruby/object:RI::Attribute 
  comment: 
  - !ruby/struct:SM::Flow::P 
    body: The Ruby code generated by ERB
  name: src
  rw: R
class_methods: 
- !ruby/object:RI::MethodSummary 
  name: new
- !ruby/object:RI::MethodSummary 
  name: version
comment: 
- !ruby/struct:SM::Flow::H 
  level: 1
  text: ERB -- Ruby Templating
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Introduction
- !ruby/struct:SM::Flow::P 
  body: ERB provides an easy to use but powerful templating system for Ruby. Using ERB, actual Ruby code can be added to any plain text document for the purposes of generating document information details and/or flow control.
- !ruby/struct:SM::Flow::P 
  body: "A very simple example is this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  require 'erb'\n\n  x = 42\n  template = ERB.new &lt;&lt;-EOF\n    The value of x is: &lt;%= x %&gt;\n  EOF\n  puts template.result(binding)\n"
- !ruby/struct:SM::Flow::P 
  body: "<em>Prints:</em> The value of x is: 42"
- !ruby/struct:SM::Flow::P 
  body: More complex examples are given below.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Recognized Tags
- !ruby/struct:SM::Flow::P 
  body: "ERB recognizes certain tags in the provided template and converts them based on the rules below:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% Ruby code -- inline with output %&gt;\n  &lt;%= Ruby expression -- replace with result %&gt;\n  &lt;%# comment -- ignored -- useful in testing %&gt;\n  % a line of Ruby code -- treated as &lt;% line %&gt; (optional -- see ERB.new)\n  %% replaced with % if first thing on a line and % processing is used\n  &lt;%% or %%&gt; -- replace with &lt;% or %&gt; respectively\n"
- !ruby/struct:SM::Flow::P 
  body: All other text is passed through ERB filtering unchanged.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Options
- !ruby/struct:SM::Flow::P 
  body: "There are several settings you can change when you use ERB:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: the nature of the tags that are recognized;
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: the value of <tt>$SAFE</tt> under which the template is run;
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: the binding used to resolve local variables in the template.
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: See the ERB.new and ERB#result methods for more detail.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Examples
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Plain Text
- !ruby/struct:SM::Flow::P 
  body: ERB is useful for any generic templating situation. Note that in this example, we use the convenient &quot;% at start of line&quot; tag, and we quote the template literally with <tt>%q{...}</tt> to avoid trouble with the backslash.
- !ruby/struct:SM::Flow::VERB 
  body: "  require &quot;erb&quot;\n\n  # Create template.\n  template = %q{\n    From:  James Edward Gray II &lt;james@grayproductions.net&gt;\n    To:  &lt;%= to %&gt;\n    Subject:  Addressing Needs\n\n    &lt;%= to[/\\w+/] %&gt;:\n\n    Just wanted to send a quick note assuring that your needs are being\n    addressed.\n\n    I want you to know that my team will keep working on the issues,\n    especially:\n\n    &lt;%# ignore numerous minor requests -- focus on priorities %&gt;\n    % priorities.each do |priority|\n      * &lt;%= priority %&gt;\n    % end\n\n    Thanks for your patience.\n\n    James Edward Gray II\n  }.gsub(/^  /, '')\n\n  message = ERB.new(template, 0, &quot;%&lt;&gt;&quot;)\n\n  # Set up template data.\n  to = &quot;Community Spokesman &lt;spokesman@ruby_community.org&gt;&quot;\n  priorities = [ &quot;Run Ruby Quiz&quot;,\n                 &quot;Document Modules&quot;,\n                 &quot;Answer Questions on Ruby Talk&quot; ]\n\n  # Produce result.\n  email = message.result\n  puts email\n"
- !ruby/struct:SM::Flow::P 
  body: <em>Generates:</em>
- !ruby/struct:SM::Flow::VERB 
  body: "  From:  James Edward Gray II &lt;james@grayproductions.net&gt;\n  To:  Community Spokesman &lt;spokesman@ruby_community.org&gt;\n  Subject:  Addressing Needs\n\n  Community:\n\n  Just wanted to send a quick note assuring that your needs are being addressed.\n\n  I want you to know that my team will keep working on the issues, especially:\n\n      * Run Ruby Quiz\n      * Document Modules\n      * Answer Questions on Ruby Talk\n\n  Thanks for your patience.\n\n  James Edward Gray II\n"
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Ruby in HTML
- !ruby/struct:SM::Flow::P 
  body: ERB is often used in <tt>.rhtml</tt> files (HTML with embedded Ruby). Notice the need in this example to provide a special binding when the template is run, so that the instance variables in the Product object can be resolved.
- !ruby/struct:SM::Flow::VERB 
  body: "  require &quot;erb&quot;\n\n  # Build template data class.\n  class Product\n    def initialize( code, name, desc, cost )\n      @code = code\n      @name = name\n      @desc = desc\n      @cost = cost\n\n      @features = [ ]\n    end\n\n    def add_feature( feature )\n      @features &lt;&lt; feature\n    end\n\n    # Support templating of member data.\n    def get_binding\n      binding\n    end\n\n    # ...\n  end\n\n  # Create template.\n  template = %{\n    &lt;html&gt;\n      &lt;head&gt;&lt;title&gt;Ruby Toys -- &lt;%= @name %&gt;&lt;/title&gt;&lt;/head&gt;\n      &lt;body&gt;\n\n        &lt;h1&gt;&lt;%= @name %&gt; (&lt;%= @code %&gt;)&lt;/h1&gt;\n        &lt;p&gt;&lt;%= @desc %&gt;&lt;/p&gt;\n\n        &lt;ul&gt;\n          &lt;% @features.each do |f| %&gt;\n            &lt;li&gt;<b>&lt;%= f %&gt;</b>&lt;/li&gt;\n          &lt;% end %&gt;\n        &lt;/ul&gt;\n\n        &lt;p&gt;\n          &lt;% if @cost &lt; 10 %&gt;\n            <b>Only &lt;%= @cost %&gt;!!!</b>\n          &lt;% else %&gt;\n             Call for a price, today!\n          &lt;% end %&gt;\n        &lt;/p&gt;\n\n      &lt;/body&gt;\n    &lt;/html&gt;\n  }.gsub(/^  /, '')\n\n  rhtml = ERB.new(template)\n\n  # Set up template data.\n  toy = Product.new( &quot;TZ-1002&quot;,\n                     &quot;Rubysapien&quot;,\n                     &quot;Geek's Best Friend!  Responds to Ruby commands...&quot;,\n                     999.95 )\n  toy.add_feature(&quot;Listens for verbal commands in the Ruby language!&quot;)\n  toy.add_feature(&quot;Ignores Perl, Java, and all C variants.&quot;)\n  toy.add_feature(&quot;Karate-Chop Action!!!&quot;)\n  toy.add_feature(&quot;Matz signature on left leg.&quot;)\n  toy.add_feature(&quot;Gem studded eyes... Rubies, of course!&quot;)\n\n  # Produce result.\n  rhtml.run(toy.get_binding)\n"
- !ruby/struct:SM::Flow::P 
  body: <em>Generates (some blank lines removed):</em>
- !ruby/struct:SM::Flow::VERB 
  body: "   &lt;html&gt;\n     &lt;head&gt;&lt;title&gt;Ruby Toys -- Rubysapien&lt;/title&gt;&lt;/head&gt;\n     &lt;body&gt;\n\n       &lt;h1&gt;Rubysapien (TZ-1002)&lt;/h1&gt;\n       &lt;p&gt;Geek's Best Friend!  Responds to Ruby commands...&lt;/p&gt;\n\n       &lt;ul&gt;\n           &lt;li&gt;<b>Listens for verbal commands in the Ruby language!</b>&lt;/li&gt;\n           &lt;li&gt;<b>Ignores Perl, Java, and all C variants.</b>&lt;/li&gt;\n           &lt;li&gt;<b>Karate-Chop Action!!!</b>&lt;/li&gt;\n           &lt;li&gt;<b>Matz signature on left leg.</b>&lt;/li&gt;\n           &lt;li&gt;<b>Gem studded eyes... Rubies, of course!</b>&lt;/li&gt;\n       &lt;/ul&gt;\n\n       &lt;p&gt;\n            Call for a price, today!\n       &lt;/p&gt;\n\n     &lt;/body&gt;\n   &lt;/html&gt;\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Notes
- !ruby/struct:SM::Flow::P 
  body: "There are a variety of templating solutions available in various Ruby projects:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: ERB's big brother, eRuby, works the same but is written in C for speed;
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: Amrita (smart at producing HTML/XML);
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: cs/Template (written in C for speed);
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere;
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: and others; search the RAA.
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: Rails, the web application framework, uses ERB to create views.
constants: 
- !ruby/object:RI::Constant 
  comment: 
  name: Revision
  value: "'$Date: 2009-02-24 02:44:50 +0900 (Tue, 24 Feb 2009) $'"
full_name: ERB
includes: []

instance_methods: 
- !ruby/object:RI::MethodSummary 
  name: def_class
- !ruby/object:RI::MethodSummary 
  name: def_method
- !ruby/object:RI::MethodSummary 
  name: def_module
- !ruby/object:RI::MethodSummary 
  name: result
- !ruby/object:RI::MethodSummary 
  name: run
- !ruby/object:RI::MethodSummary 
  name: set_eoutvar
name: ERB
superclass: Object