Module: Sinatra::ContentFor2

Defined in:
lib/sinatra/content_for2.rb,
lib/sinatra/content_for2.rb,
lib/sinatra/content_for2/version.rb

Defined Under Namespace

Modules: CurrentTemplateEngine Classes: BaseHandler, ErbHandler, HamlHandler, SlimHandler

Constant Summary collapse

VERSION =
'0.3'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



3
4
5
# File 'lib/sinatra/content_for2.rb', line 3

def self.included(base) #:nodoc:
  base.send(:include, CurrentTemplateEngine) unless base.method_defined?(:current_template_engine)
end

Instance Method Details

#capture_html(*args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sinatra/content_for2.rb', line 26

def capture_html(*args, &block)
  handler = find_proper_handler
  captured_html = nil
  if handler && handler.is_type? && handler.block_is_type?(block)
    captured_html = handler.capture_from_template(*args, &block)
  end
  if captured_html.nil? && block_given?
    captured_html = block.call(*args)
  end
  captured_html || ''
end

#content_for(key, content = nil, &block) ⇒ Object

Capture a block of content to be rendered later. For example:

<% content_for :head do %>
  <script type="text/javascript" src="/foo.js"></script>
<% end %>

You can call content_for multiple times with the same key (in the example :head), and when you render the blocks for that key all of them will be rendered, in the same order you captured them.

Your blocks can also receive values, which are passed to them by yield_content



51
52
53
54
55
56
57
58
59
60
# File 'lib/sinatra/content_for2.rb', line 51

def content_for(key, content = nil, &block)
  key = key.to_sym
  unless content.nil?
    content_blocks[key] << content
  end
  if block_given?
    content_blocks[key] << block
  end
  ''
end

#content_for?(key) ⇒ Boolean

Check if a block of content with the given key was defined. For example:

<% content_for :head do %>
  
<% end %>

<% if content_for? :head %>
  content "head" was defined.
<% end %>

Returns:

  • (Boolean)


72
73
74
# File 'lib/sinatra/content_for2.rb', line 72

def content_for?(key)
  content_blocks[key.to_sym].any?
end

#yield_content(key, *args) ⇒ Object

Render the captured blocks for a given key. For example:

<head>
  <title>Example</title>
  <% yield_content :head %>
</head>

Would render everything you declared with content_for :head before closing the tag.

You can also pass values to the content blocks by passing them as arguments after the key:

<% yield_content :head, 1, 2 %>

Would pass 1 and 2 to all the blocks registered for :head.

NOTICE that you call this without an = sign. IE, in a <% %> block, and not in a <%= %> block.

NOTICE if you call from erubis, you call this with = sign.IE, in a <%= %> block, and not in a <% %> block.



100
101
102
103
104
105
106
# File 'lib/sinatra/content_for2.rb', line 100

def yield_content(key, *args)
  blocks = content_blocks[key.to_sym]
  return nil if blocks.empty?
  blocks.map do |block|
    block.kind_of?(Proc) ? capture_html(*args, &block) : block.to_s
  end.join('')
end