Sha256: f7a1f84e6236a3ec56f5be3237c1a6d9a57fd96600de7be9b6216e0a64088f38

Contents?: true

Size: 1.53 KB

Versions: 34

Compression:

Stored size: 1.53 KB

Contents

module SinatraMore
  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 = options.delete(:content)
      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 << (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

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
sinatra_more-0.3.29 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.28 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.27 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.26 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.25 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.24 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.23 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.22 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.21 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.18 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.16 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.15 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.14 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.13 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.12 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.11 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.10 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.9 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.8 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.3.7 lib/sinatra_more/markup_plugin/tag_helpers.rb