Class: Roger::Template

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Template

Returns a new instance of Template.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :source_path (String, Pathname)

    The path to the source of the template being processed

  • :layouts_path (String, Pathname)

    The path to where all layouts reside

  • :partials_path (String, Pathname)

    The path to where all partials reside



36
37
38
39
40
41
42
43
44
45
# File 'lib/roger/template.rb', line 36

def initialize(source, options = {})
  @options = options
  self.source_path = options[:source_path]
  self.data, self.source = extract_front_matter(source)
  self.template = Tilt.new(self.source_path.to_s){ self.source }
  
  if self.data[:layout] && layout_template_path = self.find_template(self.data[:layout], :layouts_path)
    @layout_template = Tilt.new(layout_template_path.to_s)
  end
end

Instance Attribute Details

#dataObject

Store the frontmatter



17
18
19
# File 'lib/roger/template.rb', line 17

def data
  @data
end

#sourceObject

The source



14
15
16
# File 'lib/roger/template.rb', line 14

def source
  @source
end

#source_pathObject

The path to the source file for this template



23
24
25
# File 'lib/roger/template.rb', line 23

def source_path
  @source_path
end

#templateObject

The actual Tilt template



20
21
22
# File 'lib/roger/template.rb', line 20

def template
  @template
end

Class Method Details

.open(path, options = {}) ⇒ Object



26
27
28
29
# File 'lib/roger/template.rb', line 26

def open(path, options = {})
  raise "Unknown file #{path}" unless File.exist?(path)
  self.new(File.read(path), options.update(:source_path => path))
end

Instance Method Details

#find_template(name, path_type) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
# File 'lib/roger/template.rb', line 65

def find_template(name, path_type)
  raise(ArgumentError, "path_type must be one of :partials_path or :layouts_path") unless [:partials_path, :layouts_path].include?(path_type)

  return nil unless @options[path_type]

  @resolvers ||= {}        
  @resolvers[path_type] ||= Resolver.new(@options[path_type])
  
  @resolvers[path_type].find_template(name, :preferred_extension => self.target_extension)
end

#render(env = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/roger/template.rb', line 47

def render(env = {})
  context = TemplateContext.new(self, env)
  
  if @layout_template
    content_for_layout = self.template.render(context, {}) # yields
    
    @layout_template.render(context, {}) do |content_for|
      if content_for
        context._content_for_blocks[content_for]
      else
        content_for_layout
      end
    end
  else
    self.template.render(context, {})
  end
end

#source_extensionObject



92
93
94
95
96
97
98
99
# File 'lib/roger/template.rb', line 92

def source_extension
  parts = File.basename(File.basename(self.source_path.to_s)).split(".")
  if parts.size > 2
    parts[-2..-1].join(".")
  else
    File.extname(self.source_path.to_s).sub(/^\./, "")
  end
end

#target_extensionObject

Try to infer the final extension of the output file.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/roger/template.rb', line 77

def target_extension
  return @target_extension if @target_extension

  if type = MIME::Types[self.target_mime_type].first
    # Dirty little hack to enforce the use of .html instead of .htm
    if type.sub_type == "html"
      @target_extension = "html"
    else
      @target_extension = type.extensions.first
    end
  else
    @target_extension = File.extname(self.source_path.to_s).sub(/^\./, "")
  end
end

#target_mime_typeObject

Try to figure out the mime type based on the Tilt class and if that doesn’t work we try to infer the type by looking at extensions (needed for .erb)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/roger/template.rb', line 103

def target_mime_type
  mime = self.template.class.default_mime_type
  return mime if mime

  path = File.basename(self.source_path.to_s)
  mime = MIME::Types.type_for(path).first
  return mime.to_s if mime

  parts = File.basename(path).split(".")
  if parts.size > 2
    mime = MIME::Types.type_for(parts[0..-2].join(".")).first
    return mime.to_s if mime
  else
    nil
  end
end