Current File : //proc/thread-self/root/proc/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 <<-EOF\n The value of x is: <%= x %>\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: " <% Ruby code -- inline with output %>\n <%= Ruby expression -- replace with result %>\n <%# comment -- ignored -- useful in testing %>\n % a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)\n %% replaced with % if first thing on a line and % processing is used\n <%% or %%> -- replace with <% or %> 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 "% at start of line" tag, and we quote the template literally with <tt>%q{...}</tt> to avoid trouble with the backslash.
- !ruby/struct:SM::Flow::VERB
body: " require "erb"\n\n # Create template.\n template = %q{\n From: James Edward Gray II <james@grayproductions.net>\n To: <%= to %>\n Subject: Addressing Needs\n\n <%= to[/\\w+/] %>:\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 <%# ignore numerous minor requests -- focus on priorities %>\n % priorities.each do |priority|\n * <%= priority %>\n % end\n\n Thanks for your patience.\n\n James Edward Gray II\n }.gsub(/^ /, '')\n\n message = ERB.new(template, 0, "%<>")\n\n # Set up template data.\n to = "Community Spokesman <spokesman@ruby_community.org>"\n priorities = [ "Run Ruby Quiz",\n "Document Modules",\n "Answer Questions on Ruby Talk" ]\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 <james@grayproductions.net>\n To: Community Spokesman <spokesman@ruby_community.org>\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 "erb"\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 << 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 <html>\n <head><title>Ruby Toys -- <%= @name %></title></head>\n <body>\n\n <h1><%= @name %> (<%= @code %>)</h1>\n <p><%= @desc %></p>\n\n <ul>\n <% @features.each do |f| %>\n <li><b><%= f %></b></li>\n <% end %>\n </ul>\n\n <p>\n <% if @cost < 10 %>\n <b>Only <%= @cost %>!!!</b>\n <% else %>\n Call for a price, today!\n <% end %>\n </p>\n\n </body>\n </html>\n }.gsub(/^ /, '')\n\n rhtml = ERB.new(template)\n\n # Set up template data.\n toy = Product.new( "TZ-1002",\n "Rubysapien",\n "Geek's Best Friend! Responds to Ruby commands...",\n 999.95 )\n toy.add_feature("Listens for verbal commands in the Ruby language!")\n toy.add_feature("Ignores Perl, Java, and all C variants.")\n toy.add_feature("Karate-Chop Action!!!")\n toy.add_feature("Matz signature on left leg.")\n toy.add_feature("Gem studded eyes... Rubies, of course!")\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: " <html>\n <head><title>Ruby Toys -- Rubysapien</title></head>\n <body>\n\n <h1>Rubysapien (TZ-1002)</h1>\n <p>Geek's Best Friend! Responds to Ruby commands...</p>\n\n <ul>\n <li><b>Listens for verbal commands in the Ruby language!</b></li>\n <li><b>Ignores Perl, Java, and all C variants.</b></li>\n <li><b>Karate-Chop Action!!!</b></li>\n <li><b>Matz signature on left leg.</b></li>\n <li><b>Gem studded eyes... Rubies, of course!</b></li>\n </ul>\n\n <p>\n Call for a price, today!\n </p>\n\n </body>\n </html>\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