Class: Template

Inherits:
Object
  • Object
show all
Defined in:
lib/den/template.rb

Instance Method Summary collapse

Constructor Details

#initialize(template_dir, template_file = nil, post_location = nil) ⇒ Template

Returns a new instance of Template.



3
4
5
6
7
# File 'lib/den/template.rb', line 3

def initialize(template_dir, template_file=nil, post_location=nil)
  @template_dir = template_dir
  load_template(template_file) if !template_file.nil?
  @post_location = post_location
end

Instance Method Details

#load_template(filename) ⇒ Object

Loads the template file into @content



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
84
85
86
87
88
89
90
91
92
# File 'lib/den/template.rb', line 59

def load_template(filename)
  template_path = File.join(@template_dir, filename)

  if File.file?(template_path)
    # Load the template file
    template_file = ''
    File.open(template_path) do |f|
      template_file = f.read
    end

    # Check if this template is an extension
    if template_file =~ /{{ extends (\S*) }}/
      # Load the parent template into content
      load_template($1)

      # Find blocks
      template_file = template_file.split("{{ endblock }}")
      blocks = {}
      for block in template_file
        if /{{ block (?<label>\S*) }}(?<content>[\s\S]*)/ =~ block
          blocks[label] = content
        end
      end

      # Combine the parent template with the blocks
      merge_templates(blocks)
    else
      # Load the file into content
      @content = template_file
    end
  else
    puts "Unable to locate template: #{@template_dir + filename}."
  end
end

#merge_templates(blocks) ⇒ Object

Combine a template with its parent



95
96
97
98
99
# File 'lib/den/template.rb', line 95

def merge_templates(blocks)
  @content.gsub!(/{{ (\S*) }}/) do |x|
    blocks[$1]
  end
end

#populate(data) ⇒ Object

Fills in the template with the data provided



53
54
55
# File 'lib/den/template.rb', line 53

def populate(data)
  @content.gsub(/\[\[ (\S*) \]\]/) { |x| data[$1] }
end

#render(obj, prev_page = nil, next_page = nil) ⇒ Object

Calls the appropriate render function



11
12
13
14
15
16
17
18
# File 'lib/den/template.rb', line 11

def render(obj, prev_page=nil, next_page=nil)
  case obj
  when Resource
    return render_resource(obj)
  when Array
    return render_index(obj, prev_page, next_page)
  end
end

#render_index(posts, prev_page, next_page) ⇒ Object

Returns the HTML of an index



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/den/template.rb', line 33

def render_index(posts, prev_page, next_page)
  # Get the HTML of each Post and combine them
  posts.collect! { |p| p.html(File.join('/', @post_location, p[:id].to_s)) }
  data = { "body" => posts.join("\n\n") }

  # Append next/previous links if necessary
  if !prev_page.nil?
    data["body"] += "<a class=\"nav\" id=\"future\" href=\"#{prev_page}\">Newer Posts</a>\n"
  end

  if !next_page.nil?
    data["body"] += "<a class=\"nav\" id=\"past\" href=\"#{next_page}\">Older Posts</a>\n"
  end

  # Return the rendered index
  populate(data)
end

#render_resource(r) ⇒ Object

Returns the HTML of a Resource



22
23
24
25
26
27
28
29
# File 'lib/den/template.rb', line 22

def render_resource(r)
  data = {
    "title" => r[:title],
    "content" => r.html
  }

  populate(data)
end