Class: Fir::PageRenderer

Inherits:
Object
  • Object
show all
Includes:
TemplateLoading
Defined in:
lib/fir/pages.rb

Instance Method Summary collapse

Constructor Details

#initializePageRenderer

Returns a new instance of PageRenderer.



36
37
38
39
40
41
42
43
# File 'lib/fir/pages.rb', line 36

def initialize
	@page_config = yaml_or_empty(File.join(FIR_ROOT, 'pages.yml'))
	@path_mappings = @page_config.inject({}) do |mappings, (page, configs)|
		mappings[configs['path']] = page if configs['path']
		mappings
	end
	@menus = yaml_or_empty(File.join(FIR_ROOT, 'menus.yml'))
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fir/pages.rb', line 45

def call(env)
	status = 200
	content_type = 'text/html'
	body = ''
	path = ::Rack::Utils.unescape(env['PATH_INFO']).chomp('/').gsub(/^\//, '')
	path = Fir.sanitize_page(path) # path now looks like 'folder/page-name'
	if @path_mappings.has_key?(path)
		path = @path_mappings[path]
	end
	template_options = {:menus => @menus, :request => Rack::Request.new(env)}
	if directory?(path)
		# This is like Apache's DirectoryIndex: if the request is for
		# a directory, look for a page called "index" in that directory.
		path = File.join(path, 'index').gsub(/^\//, '') # path now looks like 'folder/index' or just 'index'
	end
	if template = load_template(path)
		body = template.render_with_layout(template_options.merge(:page_config => page_config(path)))
		if Fir.config.perform_caching
			cache_result(path, body)
		end
	elsif template = load_template('404')
		# Page not found. Render 404 template.
		status = 404
		body = template.render_with_layout(template_options)
	else
		# Page not found, and neither was the 404 template! Render a simple message.
		content_type = 'text/plain'
		status = 404
		body = 'Sorry, there is nothing at this address. (404 Not Found)'
	end
	[status, {'Content-Type' => content_type}, body ]
end