Module: Nanoc::Helpers::Rendering

Includes:
Capturing
Defined in:
lib/nanoc/helpers/rendering.rb

Overview

Instance Method Summary collapse

Methods included from Capturing

#capture, #content_for

Instance Method Details

#render(identifier, other_assigns = {}, &block) ⇒ String?

Parameters:

  • identifier (String)
  • other_assigns (Hash) (defaults to: {})

Returns:

  • (String, nil)

Raises:

  • (Nanoc::Int::Errors::UnknownLayout)
  • (Nanoc::Int::Errors::CannotDetermineFilter)
  • (Nanoc::Int::Errors::UnknownFilter)


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
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
# File 'lib/nanoc/helpers/rendering.rb', line 14

def render(identifier, other_assigns = {}, &block)
  # Find layout
  layout_view = @layouts[identifier]
  layout_view ||= @layouts[identifier.__nanoc_cleaned_identifier]
  raise Nanoc::Int::Errors::UnknownLayout.new(identifier) if layout_view.nil?
  layout = layout_view.unwrap

  # Visit
  dependency_tracker = @config._context.dependency_tracker
  dependency_tracker.bounce(layout)

  # Capture content, if any
  captured_content = block_given? ? capture(&block) : nil

  # Get assigns
  assigns = {
    content: captured_content,
    item: @item,
    item_rep: @item_rep,
    items: @items,
    layout: layout_view,
    layouts: @layouts,
    config: @config,
  }.merge(other_assigns)

  # Get filter name
  filter_name, filter_args = *@config._context.compiler.filter_name_and_args_for_layout(layout)
  raise Nanoc::Int::Errors::CannotDetermineFilter.new(layout.identifier) if filter_name.nil?

  # Get filter class
  filter_class = Nanoc::Filter.named(filter_name)
  raise Nanoc::Int::Errors::UnknownFilter.new(filter_name) if filter_class.nil?

  # Create filter
  filter = filter_class.new(assigns)

  begin
    # Notify start
    Nanoc::Int::NotificationCenter.post(:processing_started, layout)

    # Layout
    content = layout.content
    arg = content.binary? ? content.filename : content.string
    result = filter.setup_and_run(arg, filter_args)

    # Append to erbout if we have a block
    if block_given?
      # Append result and return nothing
      erbout = eval('_erbout', block.binding)
      erbout << result
      ''
    else
      # Return result
      result
    end
  ensure
    # Notify end
    Nanoc::Int::NotificationCenter.post(:processing_ended, layout)
  end
end