1 class PageContext < Radius::Context
2
3 attr_reader :page
4
5 def initialize(page)
6 super
7 @page = page
8 globals.page = @page
9 page.tags.each do |name|
10 define_tag(name) { |tag_binding| page.render_tag(name, tag_binding) }
11 end
12 end
13
14 def dup
15 rv = self.class.new(page)
16 rv.globals = globals.dup
17 rv.definitions = definitions.dup
18 rv
19 end
20
21 def render_tag(name, attributes = {}, &block)
22 binding = @tag_binding_stack.last
23 locals = binding ? binding.locals : globals
24 set_process_variables(locals.page)
25 super
26 rescue Exception => e
27 raise e if raise_errors?
28 @tag_binding_stack.pop unless @tag_binding_stack.last == binding
29 render_error_message(e.message)
30 end
31
32 def tag_missing(name, attributes = {}, &block)
33 super
34 rescue Radius::UndefinedTagError => e
35 raise StandardTags::TagError.new(e.message)
36 end
37
38 private
39
40 def render_error_message(message)
41 "<div><strong>#{message}</strong></div>"
42 end
43
44 def set_process_variables(page)
45 page.request ||= @page.request
46 page.response ||= @page.response
47 end
48
49 def raise_errors?
50 RAILS_ENV != 'production'
51 end
52
53 end