Class: Middleman::Sitemap::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-sculptor/resource_patch.rb

Instance Method Summary collapse

Instance Method Details

#render(opts = {}, locs = {}, &block) ⇒ String

Render this resource

Returns:

  • (String)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/middleman-sculptor/resource_patch.rb', line 9

def render(opts={}, locs={}, &block)
  return app.template_data_for_file(source_file) unless template?

  relative_source = Pathname(source_file).relative_path_from(Pathname(app.root))

  instrument 'render.resource', path: relative_source, destination_path: destination_path  do
    md   = .dup
    opts = md[:options].deep_merge(opts)

    # Pass "renderer_options" hash from frontmatter along to renderer
    if md[:page]['renderer_options']
      opts[:renderer_options] = {}
      md[:page]['renderer_options'].each do |k, v|
        opts[:renderer_options][k.to_sym] = v
      end
    end

    locs = md[:locals].deep_merge(locs)

    # Forward remaining data to helpers
    app.data.store('page', md[:page]) if md.key?(:page)

    blocks = Array(md[:blocks]).dup
    blocks << block if block_given?

    # PATCH
    # reset app.current_path to current resource on each render
    # so that a prerendered resource has the correct mappings to original resource
    app.current_path = destination_path

    # Certain output file types don't use layouts
    unless opts.key?(:layout)
      opts[:layout] = false if %w(.js .json .css .txt).include?(ext)
    end

    app.render_template(source_file, locs, opts, blocks)
  end
end