Sha256: dd39f26ef15ad80df45f7b9f124dba95866f7d7574edae9b0d6129c9a91ce98b

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

module Merb
  module Helpers
    module Tag    
      # Creates a generic HTML tag. You can invoke it a variety of ways.
      #   
      #   tag :div
      #   # <div></div>
      #   
      #   tag :div, 'content'
      #   # <div>content</div>
      #   
      #   tag :div, :class => 'class'
      #   # <div class="class"></div>
      #   
      #   tag :div, 'content', :class => 'class'
      #   # <div class="class">content</div>
      #   
      #   tag :div do
      #     'content'
      #   end
      #   # <div>content</div>
      #   
      #   tag :div, :class => 'class' do
      #     'content'
      #   end
      #   # <div class="class">content</div>
      # 
      def tag(name, contents = nil, attrs = {}, &block)
        attrs, contents = contents, nil if contents.is_a?(Hash)
        contents = capture(&block) if block_given?
        open_tag(name, attrs) + contents.to_s + close_tag(name)
      end
    
      # Creates the opening tag with attributes for the provided +name+
      # attrs is a hash where all members will be mapped to key="value"
      #
      # Note: This tag will need to be closed
      def open_tag(name, attrs = nil)
        "<#{name}#{' ' + attrs.to_html_attributes if attrs && !attrs.empty?}>"
      end
    
      # Creates a closing tag
      def close_tag(name)
        "</#{name}>"
      end
    
      # Creates a self closing tag.  Like <br/> or <img src="..."/>
      #
      # +name+ : the name of the tag to create
      # +attrs+ : a hash where all members will be mapped to key="value"
      def self_closing_tag(name, attrs = nil)
        "<#{name}#{' ' + attrs.to_html_attributes if attrs && !attrs.empty?}/>"
      end
        
    end
  end
end

class Merb::Controller
  include Merb::Helpers::Tag
end    

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
merb_helpers-0.9.6 lib/merb_helpers/tag_helpers.rb
merb_helpers-0.9.5 lib/merb_helpers/tag_helpers.rb
merb_helpers-0.9.7 lib/merb_helpers/tag_helpers.rb
merb_helpers-0.9.4 lib/merb_helpers/tag_helpers.rb
thorero-helpers-0.9.4 lib/merb_helpers/tag_helpers.rb