Sha256: 006103474c3c1d4708cc9086457bbdd7fcbd41e711e7b4a211f5ee10d769b374

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module Sinatra
  module Flash
    module Style

      # A view helper for rendering flash messages to HTML with reasonable CSS structure. Handles
      # multiple flash messages in one request. Wraps them in a <div> tag with id #flash containing
      # a <div> for each message with classes of .flash and the message type.  E.g.:
      #
      # @example
      #   <div id='flash'>
      #     <div class='flash info'>Today is Tuesday, April 27th.</div>
      #     <div class='flash warning'>Missiles are headed to destroy the Earth!</div>
      #   </div>
      #
      # It is your responsibility to style these classes the way you want in your stylesheets.
      #
      # @param[optional, String, Symbol] key Specifies which flash collection you want to display.
      #   If you use this, the collection key will be appended to the top-level div id (e.g.,
      #   'flash_login' if you pass a key of  :login).
      #
      # @return [String] Styled HTML if the flash contains messages, or an empty string if it's empty.
      #
      def styled_flash(key=:flash)
        return '' if flash(key).empty?
        id = (key == :flash ? 'flash' : "flash_#{key}")
        messages = flash(key).collect { |message| "  <div class='alert alert-#{message[0]}'>#{message[1]}</div>\n" }
        "<div id='#{id}'>\n" + messages.join + '</div>'
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gh-preview-1.0.0 lib/sinatra/flash/style_patch.rb