lib/city_watch/util/renderer.rb in city-watch-0.6.3 vs lib/city_watch/util/renderer.rb in city-watch-0.6.4

- old
+ new

@@ -1,9 +1,16 @@ +require 'erubis' + +TemplateCache = {} +ViewPath = File.expand_path(File.dirname(__FILE__) + "/../../../views") +Layout = Proc.new { + file_path = "#{ViewPath}/layouts/default.html.erb" + Erubis::FastEruby.new(File.read(file_path), bufvar: '@output_buffer', filename: file_path) +}.call + module Renderer - require 'erubis' - def render(*tpl) layout do render_bare(*tpl) end end @@ -13,38 +20,29 @@ end private def layout(&block) - @layout ||= Proc.new { - file_path = "#{view_path}/layouts/default.html.erb" - Erubis::FastEruby.new(File.read(file_path), bufvar: '@output_buffer', filename: file_path) - }.call - @layout.result(binding) + Layout.result(binding) end def template_content(file_path) return "" unless file_path File.read(file_path) end - def template_key(*keys) - keys.inject("") {|a,k| a += k.to_s} - end - def template_path(*tpl) tpl.map {|t| "#{view_path}/#{t}.html.erb" }.detect{|p| File.exists?(p) } end def template(*tpl) - return @templates[tpl.join] if @templates && @templates[tpl.join] + return TemplateCache[tpl.join] if TemplateCache[tpl.join] file_path = template_path(*tpl) return template_content(file_path) if CityWatch.debug? - @templates ||= {} - @templates[tpl.join] ||= template_content(file_path) + TemplateCache[tpl.join] ||= template_content(file_path) end def view_path - File.expand_path(File.dirname(__FILE__) + "/../../../views") + ViewPath end end \ No newline at end of file