-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathtemplate.rb
More file actions
37 lines (30 loc) · 872 Bytes
/
template.rb
File metadata and controls
37 lines (30 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'erb'
require 'ostruct'
require 'hashie'
module LinkedIn
class TemplateBinding < ::Hashie::Mash
include ERB::Util
end
module Template
class << self
cache = {}
mutex = Mutex.new
define_method :load_template do |template|
return cache[template] if cache[template]
mutex.synchronize do
return cache[template] if cache[template]
file = File.join(LinkedIn.templates, "#{template.to_s}.xml.erb")
io = ::IO.respond_to?(:binread) ? ::IO.binread(file) : ::IO.read(file)
erb = ERB.new(io)
erb.filename = file
cache[template] = erb
end
end
end
def render template, data
template = Template.load_template template
namespace = TemplateBinding.new data
template.result namespace.instance_eval { binding }
end
end
end