lib/brochure/context.rb in brochure-0.5.0 vs lib/brochure/context.rb in brochure-0.5.1

- old
+ new

@@ -1,9 +1,7 @@ module Brochure class Context - include Tilt::CompileSite - def self.for(helpers) context = Class.new(self) context.send(:include, *helpers) if helpers.any? context end @@ -33,28 +31,46 @@ end def render(logical_path, locals = {}, &block) if partial = application.find_partial(logical_path, template.format_extension) if block_given? - print partial.render(env, locals) { capture(&block) } + concat partial.render(env, locals) { capture(&block) } else partial.render(env, locals) end else raise TemplateNotFound, "no such template '#{logical_path}'" end end - def print(str) - @_out_buf << str + def engine_name + template.engine_extension[1..-1] end - def capture - buf = "" - old_buf, @_out_buf = @_out_buf, buf - yield - buf - ensure - @_out_buf = old_buf + def concat(str) + if respond_to?(method = "#{engine_name}_concat") + send(method, str) + elsif @_out_buf + @_out_buf << str + else + raise CaptureNotSupported, "no capture support for #{engine_name} templates" + end + end + + def capture(&block) + if respond_to?(method = "capture_#{engine_name}") + send(method, &block) + elsif @_out_buf + begin + buf = "" + old_buf, @_out_buf = @_out_buf, buf + yield + buf + ensure + @_out_buf = old_buf + end + else + raise CaptureNotSupported, "no capture support for #{engine_name} templates" + end end end end