タイトル: eRuby
SEOタイトル: eRuby とは?Ruby のテンプレートエンジン(ERB / Erubi 構文・タグ・サンプル)
この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;background:#fffbea;border:1px solid #f4c430;margin:1em 0;width:100%"><tr><td style="background:#f4c430;color:#000;font-weight:bold;padding:8px">この記事の要点</td></tr><tr><td style="padding:10px"><ul><li><strong>eRuby</strong> (embedded Ruby) は HTML などのテキスト中に <strong>Ruby コードを埋め込む</strong>テンプレート技術</li><li>標準実装は <strong>ERB</strong>(Ruby 標準ライブラリ)と高速版の <strong>Erubi</strong> / <strong>Erubis</strong></li><li>4 種のタグ: <code><% %></code> (実行のみ) / <code><%= %></code> (出力) / <code><%# %></code> (コメント) / <code><%- -%></code> (改行削除)</li><li>Rails のビュー (<code>.html.erb</code>) は内部で<strong>Erubi で処理</strong> — XSS 対策の自動エスケープあり</li><li>主な用途: Rails / Sinatra のビュー、メール本文の生成、設定ファイルのテンプレート化</li></ul></td></tr></table>
<h2>eRuby とは</h2>
<p><strong>eRuby</strong> (embedded Ruby) は、HTML や設定ファイルなどの<strong>テキスト中に Ruby コードを埋め込む</strong>ためのテンプレート技術の総称です。<code><% %></code> のような特殊タグでマークアップ中にロジックを書き、結果のテキストを生成します。</p>
<p>代表実装に Ruby 標準の <strong>ERB</strong>、高速な <strong>Erubi</strong> / <strong>Erubis</strong> があります。Ruby on Rails のビュー (<code>.html.erb</code>) では現在 <strong>Erubi</strong> が使われています。</p>
<h2>4 種類のタグ</h2>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
<thead><tr style="background:#e3f2fd"><th>タグ</th><th>意味</th><th>出力</th></tr></thead>
<tbody>
<tr><td><code><% Ruby %></code></td><td>Ruby コードを実行(結果は出力しない)</td><td>なし</td></tr>
<tr><td><code><%= 式 %></code></td><td>式の結果を文字列として出力</td><td>あり</td></tr>
<tr><td><code><%# コメント %></code></td><td>テンプレート上のコメント</td><td>なし</td></tr>
<tr><td><code><%- -%></code></td><td>前後の余分な改行・空白を抑制</td><td>抑制</td></tr>
</tbody>
</table>
<h2>基本サンプル (ERB)</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-ruby">require 'erb'
template = <<~ERB
<h1><%= title %></h1>
<ul>
<% items.each do |i| %>
<li><%= i %></li>
<% end %>
</ul>
ERB
title = "果物リスト"
items = ["りんご", "みかん", "ぶどう"]
erb = ERB.new(template)
puts erb.result(binding)</code></pre><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html"><!-- 出力 -->
<h1>果物リスト</h1>
<ul>
<li>りんご</li>
<li>みかん</li>
<li>ぶどう</li>
</ul></code></pre>
<h2>ファイルからテンプレートを読む</h2>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-ruby"># template.html.erb
# <h1>こんにちは、<%= name %> さん!</h1>
template = File.read('template.html.erb')
name = "Alice"
puts ERB.new(template).result(binding)</code></pre>
<h2>余白制御 (trim_mode)</h2>
<p>ERB.new の第 2 引数 (Ruby 2.6+ は <code>trim_mode:</code> キーワード) で<strong>改行・空白の扱い</strong>を指定できます。</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-ruby"># "-" モード: <%- -%> で改行抑制
erb = ERB.new(template, trim_mode: '-')
# ">" モード: %> 直後の改行を削除
erb = ERB.new(template, trim_mode: '>')
# "<>" モード: 行頭/行末の余分な改行を削除
erb = ERB.new(template, trim_mode: '<>')
# "%" モード: 行頭 % で始まる行を Ruby とみなす
erb = ERB.new(template, trim_mode: '%')</code></pre>
<h2>Rails での使い方</h2>
<p>Rails のビューは <code>app/views/コントローラ名/アクション名.html.erb</code> に置きます。コントローラのインスタンス変数 (<code>@user</code> 等) がそのままビューで使えます。</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-ruby"># app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
end</code></pre><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html"><!-- app/views/users/show.html.erb -->
<h1><%= @user.name %></h1>
<p>Email: <%= @user.email %></p>
<% if @user.admin? %>
<p><strong>管理者です</strong></p>
<% end %>
<%# このコメントはレンダリングされない %></code></pre>
<h2>自動エスケープ (XSS 対策)</h2>
<p>Rails の <code>.html.erb</code> では <code><%= %></code> が<strong>自動で HTML エスケープ</strong>されます。ユーザー入力をそのまま表示しても <script> が挿入できません。</p>
<pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-html"><%# user.name に "<script>alert(1)</script>" が入っていても安全 %>
<p>名前: <%= @user.name %></p>
<!-- 出力: <p>名前: <script>alert(1)</script></p> -->
<%# 意図的にエスケープしない (信頼できる HTML のみ) %>
<div><%= raw @article.body_html %></div>
<div><%== @article.body_html %></div> <!-- == は raw のショートカット --></code></pre>
<h2>ERB vs Erubi vs Erubis</h2>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
<thead><tr style="background:#e3f2fd"><th>実装</th><th>特徴</th><th>主な利用</th></tr></thead>
<tbody>
<tr><td><strong>ERB</strong></td><td>Ruby 標準ライブラリ。シンプル</td><td>スクリプト / メールテンプレ</td></tr>
<tr><td><strong>Erubi</strong></td><td>Erubis の後継。<strong>高速 + 自動エスケープ</strong></td><td>Rails 5.1+ のデフォルト</td></tr>
<tr><td><strong>Erubis</strong></td><td>歴史的に高速。メンテ終了</td><td>古い Rails アプリ</td></tr>
</tbody>
</table>
<h2>主な用途</h2>
<ul>
<li><strong>Rails / Sinatra のビュー</strong>: HTML 出力の定番</li>
<li><strong>メール本文の生成</strong>: ActionMailer の <code>.text.erb</code> / <code>.html.erb</code></li>
<li><strong>設定ファイルのテンプレート化</strong>: Chef / Puppet などの構成管理ツールが採用</li>
<li><strong>静的サイトジェネレータ</strong>: Middleman など</li>
<li><strong>コード生成</strong>: Rails の <code>generate</code> がスキャフォールドに ERB を使う</li>
</ul>
<h2>FAQ</h2>
<p><strong>Q: ERB と Haml / Slim どちらを使う?</strong><br />
A: ERB は HTML 知識がそのまま使え学習コストが低い。Haml/Slim は記述量が少ないが独自記法の学習が必要。<strong>新規 Rails は ERB がほぼ標準</strong>。</p>
<p><strong>Q: パフォーマンスは?</strong><br />
A: 標準 ERB は遅め。Rails の本番運用なら Erubi に切り替わっておりキャッシュも効くので<strong>体感は十分高速</strong>。</p>
<p><strong>Q: テンプレート内で複雑なロジックを書きたい</strong><br />
A: アンチパターン。<strong>ヘルパー</strong>や<strong>プレゼンター</strong> (Draper など) にロジックを移すべき。ビューは表示のみが理想。</p>