20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/dimples/publishable.rb', line 20
def render(context = {}, body = nil, use_layout = true)
class_name = self.class.name.split('::').last.downcase.to_sym
context[:site] = @site unless context[:site]
context[class_name] = self unless context[class_name]
scope = Object.new
context.each_pair do |key, value|
scope.instance_variable_set("@#{key}".to_sym, value)
end
proc = Proc.new { |template| contents() }
renderer = if @path
extension = File.extname(@path)[1..-1]
options = @site.config['rendering'][extension] || {}
Tilt.new(@path, options, &proc)
else
Tilt::StringTemplate.new(&proc)
end
begin
output = renderer.render(scope) { body }.strip
@rendered_contents = output
rescue RuntimeError, TypeError, NoMethodError, SyntaxError, NameError => e
problem_file = if @path
@path.gsub(@site.source_paths[:root], '')
else
"dynamic #{self.class}"
end
raise Errors::RenderingError.new(problem_file, e.message)
end
if use_layout && @layout && @site.templates[@layout]
output = @site.templates[@layout].render(context, output)
end
output
end
|