Class: Roger::Renderer
- Inherits:
-
Object
- Object
- Roger::Renderer
- Defined in:
- lib/roger/renderer.rb
Overview
Roger Renderer class
The renderer will set up an environment so you can consistently render templates within that environment
Constant Summary collapse
- MAX_ALLOWED_TEMPLATE_NESTING =
10
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#template_nesting ⇒ Object
readonly
Returns the value of attribute template_nesting.
Class Method Summary collapse
-
.helper(mod) ⇒ Object
Register a helper module that should be included in every template context.
- .helpers ⇒ Object
- .source_extension_for(path) ⇒ Object
-
.target_extension_for(path) ⇒ Object
Try to infer the final extension of the output file.
-
.target_mime_type_for(path) ⇒ 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).
Instance Method Summary collapse
-
#current_template ⇒ Object
The current template being rendered.
-
#initialize(env = {}, options = {}) ⇒ Renderer
constructor
A new instance of Renderer.
-
#parent_template ⇒ Object
The parent template in the nesting.
-
#render(path, options = {}, &block) ⇒ Object
The render function.
-
#render_file(path, options = {}) ⇒ Object
Render any file on disk.
Constructor Details
#initialize(env = {}, options = {}) ⇒ Renderer
Returns a new instance of Renderer.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/roger/renderer.rb', line 84 def initialize(env = {}, = {}) @options = @context = prepare_context(env) @paths = { partials: [@options[:partials_path]].flatten, layouts: [@options[:layouts_path]].flatten } # State data. Whenever we render a new template # we need to update: # # - data from front matter # - template_nesting # - current_template @data = {} @template_nesting = [] end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
81 82 83 |
# File 'lib/roger/renderer.rb', line 81 def data @data end |
#template_nesting ⇒ Object (readonly)
Returns the value of attribute template_nesting.
82 83 84 |
# File 'lib/roger/renderer.rb', line 82 def template_nesting @template_nesting end |
Class Method Details
.helper(mod) ⇒ Object
Register a helper module that should be included in every template context.
16 17 18 19 |
# File 'lib/roger/renderer.rb', line 16 def helper(mod) @helpers ||= [] @helpers << mod end |
.helpers ⇒ Object
21 22 23 |
# File 'lib/roger/renderer.rb', line 21 def helpers @helpers || [] end |
.source_extension_for(path) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/roger/renderer.rb', line 39 def source_extension_for(path) parts = File.basename(File.basename(path.to_s)).split(".") if parts.size > 2 parts[-2..-1].join(".") else File.extname(path.to_s).sub(/^\./, "") end end |
.target_extension_for(path) ⇒ Object
Try to infer the final extension of the output file.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/roger/renderer.rb', line 26 def target_extension_for(path) if type = MIME::Types[target_mime_type_for(path)].first # Dirty little hack to enforce the use of .html instead of .htm if type.sub_type == "html" "html" else type.extensions.first end else File.extname(path.to_s).sub(/^\./, "") end end |
.target_mime_type_for(path) ⇒ 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)
50 51 52 53 54 55 56 57 |
# File 'lib/roger/renderer.rb', line 50 def target_mime_type_for(path) mime = mime_type_from_template(path) || mime_type_from_filename(path) || mime_type_from_sub_extension(path) mime.to_s if mime end |
Instance Method Details
#current_template ⇒ Object
The current template being rendered
165 166 167 |
# File 'lib/roger/renderer.rb', line 165 def current_template template_nesting.last end |
#parent_template ⇒ Object
The parent template in the nesting.
170 171 172 |
# File 'lib/roger/renderer.rb', line 170 def parent_template template_nesting[-2] end |
#render(path, options = {}, &block) ⇒ Object
The render function
The render function will take care of rendering the right thing in the right context. It will:
-
Wrap templates with layouts if it’s defined in the frontmatter and load them from the right layout path.
-
Render only partials if called from within an existing template
If you just want to render an arbitrary file, use #render_file instead
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/roger/renderer.rb', line 117 def render(path, = {}, &block) template, layout = template_and_layout_for_render(path, ) # Set new current template template_nesting.push(template) # Copy data to our data store. A bit clunky; as this should be inherited @data = {}.update(@data).update(template.data) # Render the template first so we have access to # it's data in the layout. render_result = template.render([:locals] || {}, &block) # Wrap it in a layout layout.render do render_result end ensure # Only pop the template from the nesting if we actually # put it on the nesting stack. template_nesting.pop if template end |
#render_file(path, options = {}) ⇒ Object
Render any file on disk. No magic. Just rendering.
A couple of things to keep in mind:
-
The file will be rendered in this rendering context
-
Does not have layouts or block style
-
When you pass a relative path and we are within another template it will be relative to that template.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/roger/renderer.rb', line 149 def render_file(path, = {}) pn = absolute_path_from_current_template(path) template = template(pn.to_s, nil) # Track rendered file also on the rendered stack template_nesting.push(template) template.render([:locals] || {}) ensure # Only pop the template from the nesting if we actually # put it on the nesting stack. template_nesting.pop if template end |