Class: Middleman::TemplateRenderer
- Inherits:
-
Object
- Object
- Middleman::TemplateRenderer
- Extended by:
- Forwardable
- Includes:
- Contracts
- Defined in:
- lib/middleman-core/template_renderer.rb
Defined Under Namespace
Classes: Cache, TemplateNotFound
Constant Summary
Constants included from Contracts
Class Method Summary collapse
Instance Method Summary collapse
-
#Hash ⇒ String
Render a template, with layout, given a path.
-
#initialize(app, path) ⇒ TemplateRenderer
constructor
A new instance of TemplateRenderer.
- #render(locs = {}, opts = {}, &block) ⇒ Object
Methods included from Contracts
Constructor Details
#initialize(app, path) ⇒ TemplateRenderer
Returns a new instance of TemplateRenderer.
34 35 36 37 |
# File 'lib/middleman-core/template_renderer.rb', line 34 def initialize(app, path) @app = app @path = path end |
Class Method Details
Instance Method Details
#Hash ⇒ String
Render a template, with layout, given a path
44 |
# File 'lib/middleman-core/template_renderer.rb', line 44 Contract Hash, Hash => String |
#render(locs = {}, opts = {}, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/middleman-core/template_renderer.rb', line 45 def render(locs={}, opts={}, &block) path = @path.dup locals = locs.dup.freeze = opts.dup extension = File.extname(path) engine = extension[1..-1].to_sym if defined?(::I18n) old_locale = ::I18n.locale ::I18n.locale = [:locale] if [:locale] # Backwards compat ::I18n.locale = [:lang] if [:lang] end # Sandboxed class for template eval context = @app.template_context_class.new(@app, locals, ) # Add extension helpers to context. @app.extensions.add_exposed_to_context(context) content = _render_with_all_renderers(path, locs, context, opts, &block) # If we need a layout and have a layout, use it if layout_file = fetch_layout(engine, ) layout_renderer = ::Middleman::FileRenderer.new(@app, layout_file[:relative_path].to_s) content = layout_renderer.render(locals, , context) { content } end # Return result content ensure # Pop all the saved variables from earlier as we may be returning to a # previous render (layouts, partials, nested layouts). ::I18n.locale = old_locale if defined?(::I18n) end |