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, sprockets_context = nil, allow_sections = true) ⇒ ViewHandler

Returns a new instance of ViewHandler.

Parameters:

  • - (String)

    the path to the template file being processed

  • -

    the sprockets context, used for asset_url bindings

  • - (Boolean)

    if the processing should default to body section



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/volt/server/html_parser/view_handler.rb', line 16

def initialize(initial_path, sprockets_context=nil, allow_sections = true)
  @original_path = initial_path
  @sprockets_context = sprockets_context
  @links = []

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

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

Instance Attribute Details

Returns the value of attribute links.



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

def links
  @links
end

#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



36
37
38
# File 'lib/volt/server/html_parser/view_handler.rb', line 36

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

#comment(comment) ⇒ Object



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

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

#end_tag(tag_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/volt/server/html_parser/view_handler.rb', line 60

def end_tag(tag_name)
  if @in_textarea && tag_name == 'textarea'
    last.close_scope
    @in_textarea = nil
  elsif tag_name[0] == ':'
    # Closing a volt tag
    last.close_scope
  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

Called from the view scope when an asset_url binding is hit.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/volt/server/html_parser/view_handler.rb', line 92

def link_asset(url, link=true)
  if @sprockets_context
    # Getting the asset_path also links to the context.
    linked_url = @sprockets_context.asset_path(url)
  else
    # When compiling on the server, we don't use sprockets (atm), so the
    # context won't exist.  Typically compiling on the server is just used
    # to test, so we simply return the url.
    linked_url = url
  end

  last << url if link

  linked_url
end

#start_section(tag_name, attributes, unary) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/volt/server/html_parser/view_handler.rb', line 72

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/volt/server/html_parser/view_handler.rb', line 40

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



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

def text(text)
  last << text
end