Skip to content

Instantly share code, notes, and snippets.

@r10r
Forked from blueheadpublishing/erb_import_subtemplates.rb
Last active December 20, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r10r/6182642 to your computer and use it in GitHub Desktop.
Save r10r/6182642 to your computer and use it in GitHub Desktop.
<html>
<body>
<p><%= @options[:title] %></p>
<%= import("chapter.html.erb") %>
</body>
</html>
<p><%= @options[:chapter] %></p>
require "erb"
# require '../../config/application'
class ErbTemplate
attr_accessor :options
def initialize(template)
@template = template
end
def render(output)
write(import(@template), output)
end
def import(file)
ERB.new(File.read(file)).result(binding)
end
def write(erb, output)
File.open(output, 'w+') do |f|
f << erb
end
end
end
require './erb_template'
ErbTemplate.new('book.html.erb').tap do |template|
template.options = {
:chapter => 'Chapter One',
:title => 'The Neverending Story'
}
template.render('book.html')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment