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) ⇒ Object

Raises:



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

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

#haml_template_render_block(view, template) ⇒ Object



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

def haml_template_render_block view, template
  proc do |view_args|
    voo = View.new(self, view, view_args, @voo)
    with_voo voo do
      haml_to_html template, view_args
    end
  end
end

#haml_template_render_block_with_locals(view, template) ⇒ Object



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

def haml_template_render_block_with_locals view, template
  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
    end
  end
end

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



71
72
73
74
# File 'lib/card/set/format/haml_views.rb', line 71

def haml_to_html haml, locals, a_binding=nil
  a_binding ||= binding
  ::Haml::Engine.new(haml).render a_binding, locals || {}
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 = ::File.read haml_template_path view
  if block_given?
    haml_temlate_render_block_with_locals view, template, &block
  else
    haml_template_render_block view, template
  end
end

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



65
66
67
68
69
# File 'lib/card/set/format/haml_views.rb', line 65

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