Module: Merb::Helpers::Tag

Included in:
GlobalHelpers, Form::Builder::Base
Defined in:
lib/merb-helpers/tag_helpers.rb

Instance Method Summary collapse

Instance Method Details

#close_tag(name) ⇒ Object

Creates a closing tag



43
44
45
# File 'lib/merb-helpers/tag_helpers.rb', line 43

def close_tag(name)
  "</#{name}>"
end

#open_tag(name, attrs = nil) ⇒ Object

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



38
39
40
# File 'lib/merb-helpers/tag_helpers.rb', line 38

def open_tag(name, attrs = nil)
  "<#{name}#{' ' + attrs.to_html_attributes unless attrs.blank?}>"
end

#self_closing_tag(name, attrs = nil) ⇒ Object

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”



51
52
53
# File 'lib/merb-helpers/tag_helpers.rb', line 51

def self_closing_tag(name, attrs = nil)
  "<#{name}#{' ' + attrs.to_html_attributes if attrs && !attrs.empty?}/>"
end

#tag(name, contents = nil, attrs = {}, &block) ⇒ Object

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>


28
29
30
31
32
# File 'lib/merb-helpers/tag_helpers.rb', line 28

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