Module: EL::ContentHelpers

Included in:
E
Defined in:
lib/el/content_helpers.rb

Instance Method Summary collapse

Instance Method Details

#capture_html(&proc) ⇒ Object

execute given content block and return the result. useful when you need to display same snippet multiple times and want it rendered only once

Examples:

assets = capture_html do
  js_tag(:jquery) +
  css_tag(:ui)
end


52
53
54
# File 'lib/el/content_helpers.rb', line 52

def capture_html &proc
  proc.call
end

#content_for(key, &proc) ⇒ Object

capture content and then render it into a different place

Examples:

content_for :assets do
  js_tag :jquery
end

p content_for?(:assets)
#=> #<Proc:0x00...

yield_content :assets
#=> '<script src="jquery.js" type="text/javascript"></script>'
content_for :account do |name, email|
  form_tag! do
    input_tag(value: name) +
    input_tag(value: email)
  end
end

yield_content :account, :foo, '[email protected]'
#=> '<form><input value="foo"><input value="[email protected]"></form>'


28
29
30
# File 'lib/el/content_helpers.rb', line 28

def content_for key, &proc
  (@__el__content_for ||= {})[key] = proc
end

#content_for?(key) ⇒ Boolean

check whether content block exists for some key

Returns:

  • (Boolean)


33
34
35
# File 'lib/el/content_helpers.rb', line 33

def content_for? key
  (@__el__content_for || {})[key]
end

#yield_content(key, *args) ⇒ Object

render a content block captured by ‘content_for`



38
39
40
# File 'lib/el/content_helpers.rb', line 38

def yield_content key, *args
  (proc = content_for?(key)) && proc.call(*args)
end