Module: Docks::Renderers::Common::Capturable

Included in:
ERB, Haml, Slim
Defined in:
lib/docks/renderers/common_features/capturable.rb

Overview

Public: adds the required methods for template languages that support capturing and reusing blocks of content in the template.

Instance Method Summary collapse

Instance Method Details

#capture(&block) ⇒ Object

Public: captures a block. Inclusions of this Mixin must override this method.

block - The block of content to capture.

Returns a String of the captured content.

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/docks/renderers/common_features/capturable.rb', line 18

def capture(&block)
  raise NotImplementedError.new("You must implement a `capture` method.")
end

#concat(content) ⇒ Object

Concatenates the ‘content` to the output buffer.

content - The content to append.

Returns the String of content passed.



28
29
30
# File 'lib/docks/renderers/common_features/capturable.rb', line 28

def concat(content)
  content
end

#content_for(name, value = nil, &block) ⇒ Object

Provides a named block to be reused later or, if only a name is provided, retrieves the captured content.

name - The name of the captured block block - The block of content to capture and store. If not provided,

the method will instead retrieve and return a previously
captured block named `name`, if one exists.

Returns nothing (in the case of a block being provided) or the previously-captured block by the passed ‘name`.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/docks/renderers/common_features/capturable.rb', line 43

def content_for(name, value = nil, &block)
  @content_blocks ||= Hash.new

  if block_given?
    @content_blocks[name] = capture(&block)
    nil
  elsif !value.nil?
    @content_blocks[name] = value
    nil
  else
    @content_blocks[name]
  end
end

#content_for?(name) ⇒ Boolean

Checks whether a block named ‘name` has been captured.

name - The block to check for.

Returns a Boolean indicating whether the named block has been captured.

Returns:

  • (Boolean)


63
64
65
# File 'lib/docks/renderers/common_features/capturable.rb', line 63

def content_for?(name)
  !@content_blocks[name].nil?
end