Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

書籍 Ruby Cookbook | eRuby

パンくず

Ruby Cookbook
eRuby

概要

eRuby

eRuby

eRubyによるテンプレート処理について。
eRubuy=JSPと同じ動きをすると思って差し支えない。
テンプレートとなるテキストにスクリプトレットでRubyのコードを埋め込んで
実行することができる。

サンプル

require 'erb'

html_template =<<EOS
<html>
    <head>
        <title><%=title%></title>
    </head>
    <body></body>
</html>
EOS

template = ERB.new(html_template)
title = "hello hoge"
puts template.result(binding)
title = "hello hoo"
puts template.result(binding)

出力

<html>
    <head>
        <title>hello hoge</title>
    </head>
    <body></body>
</html>
<html>
    <head>
        <title>hello hoo</title>
    </head>
    <body></body>
</html>