Module: Nanoc::Helpers::Capturing

Included in:
Filtering, HTMLEscape, Rendering
Defined in:
lib/nanoc/helpers/capturing.rb

Overview

Provides functionality for “capturing” content in one place and reusing this content elsewhere.

For example, suppose you want the sidebar of your site to contain a short summary of the item. You could put the summary in the meta file, but that’s not possible when the summary contains eRuby. You could also put the sidebar inside the actual item, but that’s not very pretty. Instead, you write the summary on the item itself, but capture it, and print it in the sidebar layout.

This helper has been tested with ERB and Haml. Other filters may not work correctly.

Examples:

Capturing content for a summary


<% content_for :summary do %>
  <p>On this item, nanoc is introduced, blah blah.</p>
<% end %>

Showing captured content in a sidebar


<div id="sidebar">
  <h3>Summary</h3>
  <%= content_for(@item, :summary) || '(no summary)' %>
</div>

Showing captured content in a sidebar the old, deprecated way (do not use or I will become very angry)


<div id="sidebar">
  <h3>Summary</h3>
  <%= @item[:content_for_summary] || '(no summary)' %>
</div>

Defined Under Namespace

Classes: CapturesStore

Instance Method Summary (collapse)

Instance Method Details

- (String) capture(&block)

Evaluates the given block and returns its contents. The contents of the block is not outputted.

Returns:

  • (String)

    The captured result



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/nanoc/helpers/capturing.rb', line 151

def capture(&block)
  # Get erbout so far
  erbout = eval('_erbout', block.binding)
  erbout_length = erbout.length

  # Execute block
  block.call

  # Get new piece of erbout
  erbout_addition = erbout[erbout_length..-1]

  # Remove addition
  erbout[erbout_length..-1] = ''

  # Depending on how the filter outputs, the result might be a
  # single string or an array of strings (slim outputs the latter).
  erbout_addition = erbout_addition.join if erbout_addition.is_a? Array

  # Done.
  erbout_addition
end

- (void) content_for(name, &block) - (String) content_for(item, name)

Overloads:

  • - (void) content_for(name, &block)

    This method returns an undefined value.

    Captures the content inside the block and stores it so that it can be referenced later on. The same method, #content_for, is used for getting the captured content as well as setting it. When capturing, the content of the block itself will not be outputted.

    For backwards compatibility, it is also possible to fetch the captured content by getting the contents of the attribute named content_for_ followed by the given name. This way of accessing captures is deprecated.

    Parameters:

    • name (Symbol, String)

      The base name of the attribute into which the content should be stored

  • - (String) content_for(item, name)

    Fetches the capture with the given name from the given item and returns it.

    Parameters:

    • item (Nanoc::Item)

      The item for which to get the capture

    • name (Symbol, String)

      The name of the capture to fetch

    Returns:

    • (String)

      The stored captured content



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nanoc/helpers/capturing.rb', line 100

def content_for(*args, &block)
  if block_given? # Set content
    # Get args
    if args.size != 1
      raise ArgumentError, 'expected 1 argument (the name ' +
        "of the capture) but got #{args.size} instead"
    end
    name = args[0]

    # Capture and store
    content = capture(&block)
    @site.captures_store[@item, name.to_sym] = content
  else # Get content
    # Get args
    if args.size != 2
      raise ArgumentError, 'expected 2 arguments (the item ' +
        "and the name of the capture) but got #{args.size} instead"
    end
    item = args[0]
    name = args[1]

    # Create dependency
    current_item = @site.compiler.dependency_tracker.top
    if item != current_item
      Nanoc::NotificationCenter.post(:visit_started, item)
      Nanoc::NotificationCenter.post(:visit_ended,   item)

      # This is an extremely ugly hack to get the compiler to recompile the
      # item from which we use content. For this, we need to manually edit
      # the content attribute to reset it. :(
      # FIXME clean this up
      if !@site.captures_store_compiled_items.include? item
        @site.captures_store_compiled_items << item
        item.forced_outdated = true
        item.reps.each do |r|
          raw_content = item.raw_content
          r.content = { :raw => raw_content, :last => raw_content }
          raise Nanoc::Errors::UnmetDependency.new(r)
        end
      end
    end

    # Get content
    @site.captures_store[item, name.to_sym]
  end
end