Module: Nanoc3::Helpers::Capturing
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.
Instance Method Summary (collapse)
-
- (String) capture(&block)
Evaluates the given block and returns the result.
-
- (void) content_for(name, &block)
Captures the content inside the block and stores it in an item attribute named
content_for_followed by the given name.
Instance Method Details
- (String) capture(&block)
Evaluates the given block and returns the result. The contents of the block is not outputted.
This function has been tested with ERB and Haml. Other filters may not work correctly.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nanoc3/helpers/capturing.rb', line 48 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] = '' # Done erbout_addition end |
- (void) content_for(name, &block)
This method returns an undefined value.
Captures the content inside the block and stores it in an item
attribute named content_for_ followed by the given name. The
content of the block itself will not be outputted.
content should be stored
37 38 39 |
# File 'lib/nanoc3/helpers/capturing.rb', line 37 def content_for(name, &block) eval("@item[:content_for_#{name.to_s}] = capture(&block)") end |