Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Rails じゃなくて Ruby で直接 erb テンプレートを利用して HTML を文字列で得る一番簡単な例 ( #ruby )

More than 1 year has passed since last update.

ファイル

some.erb

<%= message %>
<%= name %>

ruby

  • File を ERB.new に渡して 変数を binding する
  • 引数も渡さずに変数が反映されているのが不思議だが、bindingってそういうことらしい
require 'erb'

message = 'hello!'
name = 'Alice'

erb = File.read('some.erb')
# => "<%= message %>\n<%= name %>\n"

ERB.new(erb).result(binding)
# => "hello!\nAlice\n"

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2641

YumaInaura
Ruby on Rails 業務経験 約5年 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible AWS など / いなうらゆうま / YumaInaura / 稲浦悠馬
http://twitter.com/yumainaura
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away