blueheadpublishing/bookshop — Issue #11: utilize <%= import() %> tags in ERB templates
Author: blueheadpublishing
Created: 2011-12-10 00:45:35+00:00
State: open

user should be able to use <%= import() %> to import other ERB templates.

see gist: https://gist.github.com/1439435

e.g.

```ruby
# source from http://snippets.dzone.com/posts/show/1723
#
# This is a script from Brad Phelan that I've edited for
# inclusion in my bookshop gem.

require "erb"

class QuickTemplate
attr_reader :args, :text
def initialize(file)
@text = File.read(file)
end
def exec(args={})
b = binding
template = ERB.new(@text, 0, "%<>")
result = template.result(b)
# Chomp the trailing newline
result.gsub(/\n$/,'')
end
end

def import(file, args={})
QuickTemplate.new(file).exec(args)
end

# write the contents to an HTML file
File.open('builds/html/book.html', 'a') do |f|
f << import(ARGV[0])
end

# book.html.erb
#
#

#

This is a test

# <%= import("chapter.html.erb") %>
#

# chapter.html.erb
#
#

Chapter One

# run script from command line
#
# $ erb_import_subtemplates.rb book.html.erb
```