Sha256: 2d3a767edbbde8855b701be3f41f81f86347f7d3d144045dade4c8d210b07824

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Renderer
	
	require 'erubis'
	
	def render(*tpl)
		layout do
			render_bare(*tpl)
		end
	end
	
	def render_bare(*tpl)
		Erubis::FastEruby.new(template(*tpl), filename: template_path(*tpl)).result(binding)
	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)
	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]
		file_path = template_path(*tpl)
		return template_content(file_path) if CityWatch.debug?
		@templates ||= {}
		@templates[tpl.join] ||= template_content(file_path)
	end
	
	def view_path
		File.expand_path(File.dirname(__FILE__) + "/../../../views")
	end
	
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
city-watch-0.6.3 lib/city_watch/util/renderer.rb