Class: Volt::ViewHandler

Inherits:
Object show all
Defined in:
lib/volt/server/html_parser/view_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_path, allow_sections = true) ⇒ ViewHandler

Returns a new instance of ViewHandler.



13
14
15
16
17
18
19
20
21
# File 'lib/volt/server/html_parser/view_handler.rb', line 13

def initialize(initial_path, allow_sections = true)
  @original_path = initial_path

  # Default to the body section
  initial_path   += '/body' if allow_sections

  @scope     = [ViewScope.new(self, initial_path)]
  @templates = {}
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'lib/volt/server/html_parser/view_handler.rb', line 3

def scope
  @scope
end

#templatesObject (readonly)

Returns the value of attribute templates.



3
4
5
# File 'lib/volt/server/html_parser/view_handler.rb', line 3

def templates
  @templates
end

Instance Method Details

#binding(binding) ⇒ Object



31
32
33
# File 'lib/volt/server/html_parser/view_handler.rb', line 31

def binding(binding)
  @scope.last.add_binding(binding)
end

#comment(comment) ⇒ Object



23
24
25
# File 'lib/volt/server/html_parser/view_handler.rb', line 23

def comment(comment)
  last << "<!--#{comment}-->"
end

#end_tag(tag_name) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/volt/server/html_parser/view_handler.rb', line 55

def end_tag(tag_name)
  if @in_textarea && tag_name == 'textarea'
    last.close_scope
    @in_textarea = nil
  else
    last << "</#{tag_name}>"
  end
end

#htmlObject



5
6
7
# File 'lib/volt/server/html_parser/view_handler.rb', line 5

def html
  last.html
end

#lastObject



9
10
11
# File 'lib/volt/server/html_parser/view_handler.rb', line 9

def last
  @scope.last
end

#start_section(tag_name, attributes, unary) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/volt/server/html_parser/view_handler.rb', line 64

def start_section(tag_name, attributes, unary)
  path = last.path
  # Start of section
  if @in_section
    # Close any previous sections
    last.close_scope
  else
    # This is the first time we've hit a section header, everything
    # outside of the headers should be removed
    @templates = {}
  end

  @in_section = tag_name[1..-1]

  # Set the new path to include the section
  new_path    = @original_path + '/' + @in_section
  @scope      = [ViewScope.new(self, new_path)]
end

#start_tag(tag_name, attributes, unary) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/volt/server/html_parser/view_handler.rb', line 35

def start_tag(tag_name, attributes, unary)
  case tag_name[0]
    when ':'
      # Component
      last.add_component(tag_name, attributes, unary)
    else
      if tag_name == 'textarea'
        @in_textarea = true
        last.add_textarea(tag_name, attributes, unary)
      else

        # Normal tag
        attributes = last.process_attributes(tag_name, attributes)
        attr_str   = last.attribute_string(attributes)

        last << "<#{tag_name}#{attr_str}#{unary ? ' /' : ''}>"
      end
  end
end

#text(text) ⇒ Object



27
28
29
# File 'lib/volt/server/html_parser/view_handler.rb', line 27

def text(text)
  last << text
end