Forked from blueheadpublishing/erb_import_subtemplates.rb
Last active
December 20, 2015 19:19
-
-
Save r10r/6182642 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<p><%= @options[:title] %></p> | |
<%= import("chapter.html.erb") %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p><%= @options[:chapter] %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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