Module: Card::Set::Format::HamlViews

Included in:
AbstractFormat
Defined in:
lib/card/set/format/haml_views.rb

Overview

Examples:

# mod/core/set/type/basic.rb
view :my_view, template: :haml  # renders mod/core/view/type/basic/my_view.haml

view :with_instance_variables, template: :haml do
  @hello = "haml"
end

# mod/core/view/type/basic/with_instance_variables.haml
Hello
  = hello

> render :with_instance_variables  # => "Hello haml"

Instance Method Summary collapse

Instance Method Details

#haml_template_path(view, source = nil) ⇒ Object

Raises:



57
58
59
60
61
62
63
64
65
66
# File 'lib/card/set/format/haml_views.rb', line 57

def haml_template_path view, source=nil
  source ||= source_location
  basename = ::File.basename(source, ".rb")
  source_dir = ::File.dirname(source)
  ["./#{basename}", "."].each do |template_dir|
    path = try_haml_template_path(template_dir, view, source_dir)
    return path if path
  end
  raise(Card::Error, "can't find haml template for #{view}")
end

#haml_template_render_block(view, template_path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/card/set/format/haml_views.rb', line 33

def haml_template_render_block view, template_path
  template = ::File.read template_path
  proc do |view_args|
    voo = View.new(self, view, view_args, @voo)
    with_voo voo do
      haml_to_html template, view_args, nil, path: template_path
    end
  end
end

#haml_template_render_block_with_locals(view, template_path, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/card/set/format/haml_views.rb', line 43

def haml_template_render_block_with_locals view, template_path, &block
  template = ::File.read template_path
  proc do |view_args|
    instance_exec view_args, &block
    locals = instance_variables.each_with_object({}) do |var, h|
      h[var.to_s.tr("@", "").to_sym] = instance_variable_get var
    end
    voo = View.new(self, view, view_args, @voo)
    with_voo voo do
      haml_to_html template, locals, nil, path: template_path
    end
  end
end

#haml_to_html(haml, locals, a_binding = nil, debug_info = {}) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/card/set/format/haml_views.rb', line 74

def haml_to_html haml, locals, a_binding=nil, debug_info={}
  a_binding ||= binding
  ::Haml::Engine.new(haml).render a_binding, locals || {}
rescue Haml::SyntaxError => e
  raise Card::Error,
        "haml syntax error #{template_location(debug_info)}: #{e.message}"
end

#haml_view_block(view, &block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/card/set/format/haml_views.rb', line 24

def haml_view_block view, &block
  template_path = haml_template_path view
  if block_given?
    haml_template_render_block_with_locals view, template_path, &block
  else
    haml_template_render_block view, template_path
  end
end

#template_location(debug_info) ⇒ Object



82
83
84
85
86
# File 'lib/card/set/format/haml_views.rb', line 82

def template_location debug_info
  return "" unless debug_info[:path]
  Pathname.new(debug_info[:path])
          .relative_path_from(Pathname.new(Dir.pwd))
end

#try_haml_template_path(template_path, view, source_dir, ext = "haml") ⇒ Object



68
69
70
71
72
# File 'lib/card/set/format/haml_views.rb', line 68

def try_haml_template_path template_path, view, source_dir, ext="haml"
  path = ::File.expand_path("#{template_path}/#{view}.#{ext}", source_dir)
               .sub(%r{(/mod/[^/]+)/set/}, "\\1/#{TEMPLATE_DIR}/")
  ::File.exist?(path) && path
end