Class: Roger::Template
- Inherits:
-
Object
- Object
- Roger::Template
- Defined in:
- lib/roger/template.rb
Overview
Roger template processing class
Instance Attribute Summary collapse
-
#data ⇒ Object
Store the frontmatter.
-
#source ⇒ Object
The source.
-
#source_path ⇒ Object
The path to the source file for this template.
-
#template ⇒ Object
The actual Tilt template.
Class Method Summary collapse
Instance Method Summary collapse
- #find_template(name, path_type) ⇒ Object
-
#initialize(source, options = {}) ⇒ Template
constructor
A new instance of Template.
- #render(env = {}) ⇒ Object
- #source_extension ⇒ Object
-
#target_extension ⇒ Object
Try to infer the final extension of the output file.
-
#target_mime_type ⇒ Object
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).
Constructor Details
#initialize(source, options = {}) ⇒ Template
Returns a new instance of Template.
35 36 37 38 39 40 41 42 43 |
# File 'lib/roger/template.rb', line 35 def initialize(source, = {}) @options = self.source_path = [:source_path] self.data, self.source = extract_front_matter(source) self.template = Tilt.new(source_path.to_s) { self.source } initialize_layout end |
Instance Attribute Details
#data ⇒ Object
Store the frontmatter
16 17 18 |
# File 'lib/roger/template.rb', line 16 def data @data end |
#source ⇒ Object
The source
13 14 15 |
# File 'lib/roger/template.rb', line 13 def source @source end |
#source_path ⇒ Object
The path to the source file for this template
22 23 24 |
# File 'lib/roger/template.rb', line 22 def source_path @source_path end |
#template ⇒ Object
The actual Tilt template
19 20 21 |
# File 'lib/roger/template.rb', line 19 def template @template end |
Class Method Details
.open(path, options = {}) ⇒ Object
25 26 27 28 |
# File 'lib/roger/template.rb', line 25 def open(path, = {}) fail "Unknown file #{path}" unless File.exist?(path) new(File.read(path), .update(source_path: path)) end |
Instance Method Details
#find_template(name, path_type) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/roger/template.rb', line 63 def find_template(name, path_type) unless [:partials_path, :layouts_path].include?(path_type) fail(ArgumentError, "path_type must be one of :partials_path or :layouts_path") end return nil unless @options[path_type] @resolvers ||= {} @resolvers[path_type] ||= Resolver.new(@options[path_type]) @resolvers[path_type].find_template(name, preferred_extension: target_extension) end |
#render(env = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/roger/template.rb', line 45 def render(env = {}) context = TemplateContext.new(self, env) if @layout_template content_for_layout = 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 template.render(context, {}) end end |
#source_extension ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/roger/template.rb', line 92 def source_extension parts = File.basename(File.basename(source_path.to_s)).split(".") if parts.size > 2 parts[-2..-1].join(".") else File.extname(source_path.to_s).sub(/^\./, "") end end |
#target_extension ⇒ Object
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[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(source_path.to_s).sub(/^\./, "") end end |
#target_mime_type ⇒ Object
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 |
# File 'lib/roger/template.rb', line 103 def target_mime_type mime = mime_type_from_template || mime_type_from_filename || mime_type_from_sub_extension mime.to_s if mime end |