File: //usr/share/ri/3.0.0/system/ERB/cdesc-ERB.ri
U:RDoc::NormalClass[iI"ERB:ET@I"Object;To:RDoc::Markup::Document:@parts[
o;;[=S:RDoc::Markup::Heading:
leveli: textI"ERB -- Ruby Templating;To:RDoc::Markup::BlankLine S; ;
i;I"Introduction;T@o:RDoc::Markup::Paragraph;[I"QERB provides an easy to use but powerful templating system for Ruby. Using ;TI"KERB, actual Ruby code can be added to any plain text document for the ;TI"Mpurposes of generating document information details and/or flow control.;T@o;
;[I"#A very simple example is this:;T@o:RDoc::Markup::Verbatim;[I"require 'erb'
;TI"
;TI"x = 42
;TI"template = ERB.new <<-EOF
;TI"# The value of x is: <%= x %>
;TI" EOF
;TI"#puts template.result(binding)
;T:@format0o;
;[I"+<em>Prints:</em> The value of x is: 42;T@o;
;[I"+More complex examples are given below.;T@S; ;
i;I"Recognized Tags;T@o;
;[I"RERB recognizes certain tags in the provided template and converts them based ;TI"on the rules below:;T@o;;[I"+<% Ruby code -- inline with output %>
;TI"3<%= Ruby expression -- replace with result %>
;TI"4<%# comment -- ignored -- useful in testing %>
;TI"N% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
;TI"J%% replaced with % if first thing on a line and % processing is used
;TI"6<%% or %%> -- replace with <% or %> respectively
;T;0o;
;[I">All other text is passed through ERB filtering unchanged.;T@S; ;
i;I"Options;T@o;
;[I"@There are several settings you can change when you use ERB:;To:RDoc::Markup::List:
@type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o;
;[I"0the nature of the tags that are recognized;;To;;0;[o;
;[I"Athe binding used to resolve local variables in the template.;T@o;
;[I"<See the ERB.new and ERB#result methods for more detail.;T@S; ;
i;I"Character encodings;T@o;
;[ I"FERB (or Ruby code generated by ERB) returns a string in the same ;TI"Hcharacter encoding as the input string. When the input string has ;TI"Ma magic comment, however, it returns a string in the encoding specified ;TI"by the magic comment.;T@o;;[
I"# -*- coding: utf-8 -*-
;TI"require 'erb'
;TI"
;TI"template = ERB.new <<EOF
;TI"<%#-*- coding: Big5 -*-%>
;TI"4 \_\_ENCODING\_\_ is <%= \_\_ENCODING\_\_ %>.
;TI" EOF
;TI"puts template.result
;T;0o;
;[I"/<em>Prints:</em> \_\_ENCODING\_\_ is Big5.;T@S; ;
i;I"
Examples;T@S; ;
i;I"Plain Text;T@o;
;[I"`ERB is useful for any generic templating situation. Note that in this example, we use the ;TI"Sconvenient "% at start of line" tag, and we quote the template literally with ;TI":<tt>%q{...}</tt> to avoid trouble with the backslash.;T@o;;[*I"require "erb"
;TI"
;TI"# Create template.
;TI"template = %q{
;TI"? From: James Edward Gray II <james@grayproductions.net>
;TI" To: <%= to %>
;TI"" Subject: Addressing Needs
;TI"
;TI" <%= to[/\w+/] %>:
;TI"
;TI"K Just wanted to send a quick note assuring that your needs are being
;TI" addressed.
;TI"
;TI"H I want you to know that my team will keep working on the issues,
;TI" especially:
;TI"
;TI"D <%# ignore numerous minor requests -- focus on priorities %>
;TI"' % priorities.each do |priority|
;TI" * <%= priority %>
;TI"
% end
;TI"
;TI"! Thanks for your patience.
;TI"
;TI" James Edward Gray II
;TI"}.gsub(/^ /, '')
;TI"
;TI"3message = ERB.new(template, trim_mode: "%<>")
;TI"
;TI"# Set up template data.
;TI"?to = "Community Spokesman <spokesman@ruby_community.org>"
;TI"%priorities = [ "Run Ruby Quiz",
;TI"( "Document Modules",
;TI"6 "Answer Questions on Ruby Talk" ]
;TI"
;TI"# Produce result.
;TI"email = message.result
;TI"puts email
;T;0o;
;[I"<i>Generates:</i>;T@o;;[I"=From: James Edward Gray II <james@grayproductions.net>
;TI"=To: Community Spokesman <spokesman@ruby_community.org>
;TI" Subject: Addressing Needs
;TI"
;TI"Community:
;TI"
;TI"TJust wanted to send a quick note assuring that your needs are being addressed.
;TI"
;TI"RI want you to know that my team will keep working on the issues, especially:
;TI"
;TI" * Run Ruby Quiz
;TI" * Document Modules
;TI") * Answer Questions on Ruby Talk
;TI"
;TI"Thanks for your patience.
;TI"
;TI"James Edward Gray II
;T;0S; ;
i;I"Ruby in HTML;T@o;
;[I"_ERB is often used in <tt>.rhtml</tt> files (HTML with embedded Ruby). Notice the need in ;TI"^this example to provide a special binding when the template is run, so that the instance ;TI"5variables in the Product object can be resolved.;T@o;;[HI"require "erb"
;TI"
;TI""# Build template data class.
;TI"class Product
;TI"0 def initialize( code, name, desc, cost )
;TI" @code = code
;TI" @name = name
;TI" @desc = desc
;TI" @cost = cost
;TI"
;TI" @features = [ ]
;TI" end
;TI"
;TI"" def add_feature( feature )
;TI" @features << feature
;TI" end
;TI"
;TI", # Support templating of member data.
;TI" def get_binding
;TI" binding
;TI" end
;TI"
;TI"
# ...
;TI" end
;TI"
;TI"# Create template.
;TI"template = %{
;TI" <html>
;TI"? <head><title>Ruby Toys -- <%= @name %></title></head>
;TI" <body>
;TI"
;TI"0 <h1><%= @name %> (<%= @code %>)</h1>
;TI" <p><%= @desc %></p>
;TI"
;TI" <ul>
;TI") <% @features.each do |f| %>
;TI"( <li><b><%= f %></b></li>
;TI" <% end %>
;TI" </ul>
;TI"
;TI" <p>
;TI"! <% if @cost < 10 %>
;TI"+ <b>Only <%= @cost %>!!!</b>
;TI" <% else %>
;TI") Call for a price, today!
;TI" <% end %>
;TI" </p>
;TI"
;TI" </body>
;TI" </html>
;TI"}.gsub(/^ /, '')
;TI"
;TI"rhtml = ERB.new(template)
;TI"
;TI"# Set up template data.
;TI"#toy = Product.new( "TZ-1002",
;TI"& "Rubysapien",
;TI"M "Geek's Best Friend! Responds to Ruby commands...",
;TI"! 999.95 )
;TI"Jtoy.add_feature("Listens for verbal commands in the Ruby language!")
;TI"@toy.add_feature("Ignores Perl, Java, and all C variants.")
;TI".toy.add_feature("Karate-Chop Action!!!")
;TI"4toy.add_feature("Matz signature on left leg.")
;TI"?toy.add_feature("Gem studded eyes... Rubies, of course!")
;TI"
;TI"# Produce result.
;TI" rhtml.run(toy.get_binding)
;T;0o;
;[I"1<i>Generates (some blank lines removed):</i>;T@o;;[I"<html>
;TI"; <head><title>Ruby Toys -- Rubysapien</title></head>
;TI" <body>
;TI"
;TI"' <h1>Rubysapien (TZ-1002)</h1>
;TI"B <p>Geek's Best Friend! Responds to Ruby commands...</p>
;TI"
;TI" <ul>
;TI"O <li><b>Listens for verbal commands in the Ruby language!</b></li>
;TI"E <li><b>Ignores Perl, Java, and all C variants.</b></li>
;TI"3 <li><b>Karate-Chop Action!!!</b></li>
;TI"9 <li><b>Matz signature on left leg.</b></li>
;TI"D <li><b>Gem studded eyes... Rubies, of course!</b></li>
;TI" </ul>
;TI"
;TI"
<p>
;TI"' Call for a price, today!
;TI" </p>
;TI"
;TI" </body>
;TI"
</html>
;T;0S; ;
i;I"
Notes;T@o;
;[I"UThere are a variety of templating solutions available in various Ruby projects. ;TI"SFor example, RDoc, distributed with Ruby, uses its own template engine, which ;TI"can be reused elsewhere.;T@o;
;[I"?Other popular engines could be found in the corresponding ;TI"M{Category}[https://www.ruby-toolbox.com/categories/template_engines] of ;TI"The Ruby Toolbox.;T:
@fileI"lib/erb.rb;T:0@omit_headings_from_table_of_contents_below0o;;[ ;@;0o;;[ ;@;0o;;[ ;@;0o;;[ ;@;0;0;0[ [
I"
encoding;TI"R;T:publicFI"lib/erb.rb;T[
I"
filename;TI"RW;T;F@#[
I"lineno;T@&;F@#[
I"src;T@";F@#[U:RDoc::Constant[i I"NOT_GIVEN;TI"ERB::NOT_GIVEN;T:private0o;;[ ;@;0@@cRDoc::NormalClass0U;[i I"ZERO_SAFE_LEVELS;TI"ERB::ZERO_SAFE_LEVELS;T;0o;;[ ;@;0@@@20[ [[I"
class;T[[;[[I"new;T@#[I"version;T@#[:protected[ [;[ [I"
instance;T[[;[[I"def_class;T@#[I"def_method;T@#[I"def_module;T@#[I"location=;T@#[I"make_compiler;T@#[I"result;T@#[I"result_with_hash;T@#[I"run;T@#[I"set_eoutvar;T@#[;[ [;[[I"new_toplevel;T@#[ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@@cRDoc::TopLevel