I’m working on implementing content_for and yield_content support in Hardwired.
The Sinatra::Contrib implementation doesn’t work, so I tried a simpler version:
module ContentFor
def content_for(key, &block)
content_blocks[key.to_sym] << block.call
return ""
end
def content_for?(key)
content_blocks[key.to_sym].any?
end
def yield_content(key, *args)
content_blocks[key.to_sym].join
end
private
def content_blocks
@content_blocks ||= Hash.new {|h,k| h[k] = [] }
end
end
Unfortunately, this repeats content (content_for seems to grab everything in the template, not just child content).
What approach should I take to implementing this?
Slim only captures child content if you use
=or==, not-.Just use
= content_for :area doinstead of- content_for :area doNote: Apparently this issue is specific to the Sinatra-style
content_forandyield_content. Apparently the more sophisticated Rails implementation manages to use buffer magic to make this possible with-as well.