Sha256: 49e15f3636f1e21d85380cb13127284c5ff384a1e0ce4c08ef9a23290c1f188f

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 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
    alias content_block_tag content_tag

    # 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)
      options = options.sort { |a, b| a.to_s <=> b.to_s }
      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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sinatra_more-0.1.5 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.1.4 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.1.3 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.1.2 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.1.1 lib/sinatra_more/markup_plugin/tag_helpers.rb
sinatra_more-0.1.0 lib/sinatra_more/markup_plugin/tag_helpers.rb