Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
el / lib / el / content_helpers.rb
Size: Mime:
module EL
  module ContentHelpers

    # capture content and then render it into a different place
    #
    # @example
    #   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>'
    #
    # @example
    #   content_for :account do |name, email|
    #     form_tag! do
    #       input_tag(value: name) +
    #       input_tag(value: email)
    #     end
    #   end
    #
    #   yield_content :account, :foo, 'foo@bar.com'
    #   #=> '<form><input value="foo"><input value="foo@bar.com"></form>'
    #
    def content_for key, &proc
      (@__el__content_for ||= {})[key] = proc
    end

    # check whether content block exists for some key
    def content_for? key
      (@__el__content_for || {})[key]
    end

    # render a content block captured by `content_for`
    def yield_content key, *args
      (proc = content_for?(key)) && proc.call(*args)
    end

    # execute given content block and return the result.
    # useful when you need to display same snippet multiple times
    # and want it rendered only once
    #
    # @example
    #   assets = capture_html do
    #     js_tag(:jquery) +
    #     css_tag(:ui)
    #   end
    #
    def capture_html &proc
      proc.call
    end
  end

end

class E
  include EL::ContentHelpers
end