Class: Middleman::TemplateRenderer

Inherits:
Object
  • Object
show all
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

Contracts::PATH_MATCHER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contracts

#Contract

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

.cacheObject



27
28
29
# File 'lib/middleman-core/template_renderer.rb', line 27

def self.cache
  @_cache ||= Cache.new
end

Instance Method Details

#HashString

Render a template, with layout, given a path

Parameters:

Returns:

  • (String)


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
  options = opts.dup

  extension = File.extname(path)
  engine = extension[1..-1].to_sym

  if defined?(::I18n)
    old_locale = ::I18n.locale
    ::I18n.locale = options[:locale] if options[:locale]

    # Backwards compat
    ::I18n.locale = options[:lang] if options[:lang]
  end

  # Sandboxed class for template eval
  context = @app.template_context_class.new(@app, locals, options)

  # 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, options)
    layout_renderer = ::Middleman::FileRenderer.new(@app, layout_file[:relative_path].to_s)
    content = layout_renderer.render(locals, options, 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