Module: Nanoc3::Helpers::Capturing
- Included in:
- Filtering, HTMLEscape, Rendering
- Defined in:
- lib/nanoc3/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.
Defined Under Namespace
Classes: CapturesStore
Instance Method Summary collapse
-
#capture(&block) ⇒ String
Evaluates the given block and returns its contents.
- #content_for(*args, &block) ⇒ Object
Instance Method Details
#capture(&block) ⇒ String
Evaluates the given block and returns its contents. The contents of the block is not outputted.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/nanoc3/helpers/capturing.rb', line 118 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 |
#content_for(name, &block) ⇒ void #content_for(item, name) ⇒ String
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/nanoc3/helpers/capturing.rb', line 88 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) CapturesStore.instance[@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] # Get content CapturesStore.instance[item, name.to_sym] end end |