Module: StaticMatic::RenderMixin

Included in:
Base
Defined in:
lib/staticmatic/mixins/render.rb

Instance Method Summary collapse

Instance Method Details

#clear_template_variables!Object

clear all scope variables except @staticmatic



4
5
6
7
8
9
# File 'lib/staticmatic/mixins/render.rb', line 4

def clear_template_variables!
  
  @scope.instance_variables.each do |var|
    @scope.instance_variable_set(var, nil) unless var == '@staticmatic' || var == :@staticmatic
  end
end

#fetch_layout_path(dir = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/staticmatic/mixins/render.rb', line 59

def fetch_layout_path(dir = nil)
  layout_path = File.join(@src_dir, "_layouts")
  declared_layout_name = @scope.instance_variable_get("@layout")

  if declared_layout_name
    path = determine_template_path declared_layout_name, '', layout_path
    unless path
      error_path = File.join(layout_path, declared_layout_name)
      raise StaticMatic::Error.new("", error_path, "Layout not found")
    end
  end

  if dir
    dir_layout_name = dir.split("/")[1]
    path ||= determine_template_path dir_layout_name, '', layout_path
  end
  path ||= determine_template_path @default_layout_name, '', layout_path

  unless path
    error_path = File.join(layout_path, @default_layout_name)
    raise StaticMatic::Error.new("", error_path, "No default layout could be found")
  end

  return path
end

#generate_partial(name, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/staticmatic/mixins/render.rb', line 38

def generate_partial(name, options = {})
  partial_dir, partial_name, partial_ext = expand_path name
  partial_name = "_#{partial_name}"

  context = File.join File.dirname(self.current_file), partial_dir
  partial_path = determine_template_path(partial_name, partial_ext, context)

  unless partial_path && File.exists?(partial_path)
    # partial not found in the current file's directory, so try the _partials folder
    context = File.join @src_dir, '_partials', partial_dir
    partial_name.sub! /^_/, ''
    partial_path = determine_template_path(partial_name, partial_ext, context)
  end

  if partial_path && File.exists?(partial_path)
    return render_template partial_path
  else
    raise StaticMatic::Error.new("", name, "Partial not found")
  end
end

#render_template(file_path) ⇒ Object

Generate html from source file: render_template(“index.haml”)



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/staticmatic/mixins/render.rb', line 13

def render_template(file_path)
  @current_file_stack.push(file_path)
  begin
    tilt_template(file_path)
  rescue StaticMatic::TemplateError => e
    raise e # re-raise inline errors
  rescue Exception => e
    raise StaticMatic::TemplateError.new(file_path, e)
  ensure
    @current_file_stack.pop
  end
end

#render_template_with_layout(file_path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/staticmatic/mixins/render.rb', line 26

def render_template_with_layout(file_path)
  @current_file_stack.push(file_path)
  begin 
    rendered_file_content = render_template(file_path)
    tilt_template_with_layout(fetch_layout_path) { rendered_file_content }
  rescue Exception => e
    render_rescue_from_error(e)
  ensure
    @current_file_stack.pop
  end
end