lib/clutterbuck/erb.rb in clutterbuck-erb-0.2.1 vs lib/clutterbuck/erb.rb in clutterbuck-erb-0.3.0
- old
+ new
@@ -28,13 +28,13 @@
@views || './views'
end
# Set or get the layout to use by default when rendering all templates.
#
- # @param l [Symbol] The layout file to use.
+ # @param l [Symbol, Array<Symbol>] The layout specification to use.
#
- # @return Symbol
+ # @return [Symbol, Array<Symbol>]
#
def layout(l=Unspecified)
if l != Unspecified
@layout = l
end
@@ -105,16 +105,19 @@
#
# @return [Array<(Integer, Array<(String, String)>, Array<#to_s>)>] a
# Rack response.
#
def erb(view, vars)
- content = erbterpreter(view).result(get_binding(vars))
+ render_chain = [@layout || self.class.layout, view].flatten.compact
- layout = @layout || self.class.layout
+ content = ""
- if layout
- content = erbterpreter(layout).result(get_binding(vars) { content.chomp })
+ until render_chain.empty?
+ view = render_chain.pop
+
+ cur_binding = get_binding(vars) { content.chomp }
+ content = erbterpreter(view).result(cur_binding)
end
[
200,
[
@@ -126,11 +129,11 @@
end
# Specify a layout to use for this request. Works identically to
# {ClassMethods#layout} in all the relevant particulars.
#
- # @param l [Symbol]
+ # @param l [Symbol, Array<Symbol>]
#
def layout(l)
@layout = l
end
@@ -139,14 +142,18 @@
def get_binding(vars)
EvalBinding.new(vars).instance_eval("::Kernel.binding")
end
def view_content(view)
- File.read(File.join(self.class.views, view.to_s + ".html.erb"))
+ File.read(view_file(view))
end
+ def view_file(view)
+ File.join(self.class.views, view.to_s + ".html.erb")
+ end
+
def erbterpreter(view)
file = view_content(view)
- ERB.new(file, 0, "%-").tap { |erb| erb.filename = file }
+ ERB.new(file, 0, "%-").tap { |erb| erb.filename = view_file(view) }
end
end