Class: Volt::ViewParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html, template_path, sprockets_context = nil) ⇒ ViewParser

Returns a new instance of ViewParser.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/volt/server/html_parser/view_parser.rb', line 13

def initialize(html, template_path, sprockets_context=nil)
  @template_path = template_path

  handler = ViewHandler.new(template_path, sprockets_context)

  SandlebarsParser.new(html, handler)

  # Close out the last scope
  last_scope = handler.scope.last

  fail "Unclosed tag in:\n#{html}" unless last_scope

  last_scope.close_scope

  @templates = handler.templates
  @links     = handler.links
end

Instance Attribute Details

Returns the value of attribute links.



11
12
13
# File 'lib/volt/server/html_parser/view_parser.rb', line 11

def links
  @links
end

#templatesObject (readonly)

Returns the value of attribute templates.



11
12
13
# File 'lib/volt/server/html_parser/view_parser.rb', line 11

def templates
  @templates
end

Instance Method Details

#code(app_reference) ⇒ Object

Generate code for the view that can be evaled.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/volt/server/html_parser/view_parser.rb', line 48

def code(app_reference)
  code = ''
  templates.each_pair do |name, template|
    binding_code = []

    if template['bindings']
      template['bindings'].each_pair do |key, value|
        binding_code << "#{key.inspect} => [#{value.join(', ')}]"
      end
    end

    binding_code = "{#{binding_code.join(', ')}}"

    code << "#{app_reference}.add_template(#{name.inspect}, #{template['html'].inspect}, #{binding_code})\n"
  end

  code
end

#dataObject

Returns a parsed version of the data (useful for backend rendering and testing)



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/volt/server/html_parser/view_parser.rb', line 33

def data
  templates = @templates.deep_clone

  templates.each_pair do |name, value|
    if value['bindings']
      value['bindings'].each_pair do |number, binding|
        value['bindings'][number] = binding.map { |code| eval(code) }
      end
    end
  end

  templates
end