Sha256: 3c99ccb40a1ed47bae11638c63d918ba5d8d73c29b9b0a8fd726f6a35250819b

Contents?: true

Size: 1.67 KB

Versions: 11

Compression:

Stored size: 1.67 KB

Contents

module Padrino
  module Helpers
    module TagHelpers
      # Creates an html input field with given type and options
      # input_tag :text, :class => "test"
      def input_tag(type, options = {})
        options.reverse_merge!(:type => type)
        tag(:input, options)
      end

      # Creates an html tag with given name, content and options
      # content_tag(:p, "hello", :class => 'light')
      # content_tag(:p, :class => 'dark') do ... end
      # parameters: content_tag(name, content=nil, options={}, &block)
      def content_tag(*args, &block)
        name = args.first
        options = args.extract_options!
        tag_html = block_given? ? capture_html(&block) : args[1]
        tag_result = tag(name, options.merge(:content => tag_html))
        block_is_template?(block) ? concat_content(tag_result) : tag_result
      end

      # Creates an html tag with the given name and options
      # tag(:br, :style => 'clear:both')
      # tag(:p, :content => "hello", :class => 'large')
      def tag(name, options={})
        content, open_tag = options.delete(:content), options.delete(:open)
        identity_tag_attributes.each { |attr| options[attr] = attr.to_s if options[attr]  }
        html_attrs = options.collect { |a, v| v.blank? ? nil : "#{a}=\"#{v}\"" }.compact.join(" ")
        base_tag = (html_attrs.present? ? "<#{name} #{html_attrs}" : "<#{name}")
        base_tag << (open_tag ? ">" : (content ? ">#{content}</#{name}>" : " />"))
      end

      protected

      # Returns a list of attributes which can only contain an identity value (i.e selected)
      def identity_tag_attributes
        [:checked, :disabled, :selected, :multiple]
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
padrino-helpers-0.6.3 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.6.2 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.6.1 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.5.0 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.4.6 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.4.5 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.2.9 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.2.6 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.2.5 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.2.2 lib/padrino-helpers/tag_helpers.rb
padrino-helpers-0.2.1 lib/padrino-helpers/tag_helpers.rb