Class: Howl::Template

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

Direct Known Subclasses

Page, Post

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, site) ⇒ Template

Returns a new instance of Template.



13
14
15
16
17
18
19
# File 'lib/howl/template.rb', line 13

def initialize(path, site)
  @site = site
  @path = Pathname.new(path)
  @relative_path = path.to_s.gsub(/^#{site.root}/, '')
  @extension = @path.extname
  load_file unless @path.binary?
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/howl/template.rb', line 11

def content
  @content
end

#extensionObject

Returns the value of attribute extension.



11
12
13
# File 'lib/howl/template.rb', line 11

def extension
  @extension
end

#pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/howl/template.rb', line 11

def path
  @path
end

#relative_pathObject

Returns the value of attribute relative_path.



11
12
13
# File 'lib/howl/template.rb', line 11

def relative_path
  @relative_path
end

#siteObject

Returns the value of attribute site.



11
12
13
# File 'lib/howl/template.rb', line 11

def site
  @site
end

#viewObject

Returns the value of attribute view.



11
12
13
# File 'lib/howl/template.rb', line 11

def view
  @view
end

#view_yamlObject

Returns the value of attribute view_yaml.



11
12
13
# File 'lib/howl/template.rb', line 11

def view_yaml
  @view_yaml
end

Class Method Details

.can_view(*viewables) ⇒ Object



7
8
9
# File 'lib/howl/template.rb', line 7

def self.can_view(*viewables)
  viewables.push(*viewables)
end

.viewablesObject



3
4
5
# File 'lib/howl/template.rb', line 3

def self.viewables
  @viewables ||= []
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
# File 'lib/howl/template.rb', line 21

def ==(other)
  self.path == other.path && self.class == other.class
end

#converterObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/howl/template.rb', line 29

def converter
  unless @converter
    converter_class = Converter.subclasses.find do |converter|
      converter.matches? @extension
    end
    converter_class ||= Converter

    @converter = converter_class.new(self)
  end

  @converter
end

#output_filenameObject



25
26
27
# File 'lib/howl/template.rb', line 25

def output_filename
  path.basename.sub(/#{extension}$/, converter.extension)
end

#render(render_view = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/howl/template.rb', line 42

def render(render_view = nil)
  render_view ||= site.view
  render_view = render_view.dup
  render_view.merge!(@view)
  rendered = converter.convert(Mustache.render(@content, render_view))
  template_name = render_view.delete("template")

  if template_name
    template = find_template(template_name)
    if template && template != self
      rendered = template.render(
          render_view.merge("content" => rendered))
    else
      puts "Warning: Template #{template_name} does not exist in file #{path}"
      rendered
    end
  end

  rendered
end