Module: Tagz
- Defined in:
- lib/tagz/tagz.rb,
lib/tagz.rb,
lib/tagz/version.rb
Overview
Tagz
Simple, unified #tag helper module. To add functionality to your tags, simply include a module and super to #create_tag like below:
module Tagz
module Labels
def create_tag name, contents, attrs, &block
label = attrs.delete :label
if label && label_tag?(name)
tag(:label, "#{label}:", :for => attrs[:name]) << super
else
super
end
end
def label_tag? name
name.to_s.in? %w( textarea input select )
end
end
end
include Tagz::Labels
With our newly included Tagz::Labels, all calls to #tag will be passed to #create_tag, in turn adding our labels when appropriate.
tag :textarea, :name => :comments, :label => 'Comments'
<label for=“comments”>Comments:</label> <textarea name=“comments”></textarea>
Defined Under Namespace
Constant Summary collapse
- SELF_CLOSING_TAGS =
– Self closing elements. ++
:input, :link, :base, :area, :br, :hr, :img, :meta
- BOOLEAN_ATTRIBUTES =
– Boolean attributes. ++
:selected, :checked, :disabled, :readonly, :multiple, :defer
- VERSION =
'1.0.1'
Class Method Summary collapse
-
.boolean_attribute?(name) ⇒ Boolean
Check if name is a boolean attribute.
-
.closing_tag(name) ⇒ Object
Return closing tag of name.
-
.create_tag(name, contents = nil, attrs = {}, &block) ⇒ Object
:stopdoc:.
-
.normalize_html_attributes(attrs = {}) ⇒ Object
Normalize attrs, replacing boolean keys with their mirrored values.
-
.open_tag(name, attrs = {}) ⇒ Object
Return an opening tag of name, with attrs.
-
.self_closing_tag(name, attrs = {}) ⇒ Object
Return a self closing tag of name, with attrs.
-
.self_closing_tag?(name) ⇒ Boolean
Check if tag name is a self-closing tag.
-
.tag(name, contents = nil, attrs = {}, &block) ⇒ Object
Return markup for tag name.
Class Method Details
.boolean_attribute?(name) ⇒ Boolean
Check if name is a boolean attribute.
106 107 108 |
# File 'lib/tagz/tagz.rb', line 106 def boolean_attribute? name name.in? BOOLEAN_ATTRIBUTES end |
.closing_tag(name) ⇒ Object
Return closing tag of name.
148 149 150 |
# File 'lib/tagz/tagz.rb', line 148 def closing_tag name "</#{name}>" end |
.create_tag(name, contents = nil, attrs = {}, &block) ⇒ Object
:stopdoc:
97 98 99 100 101 |
# File 'lib/tagz/tagz.rb', line 97 def create_tag name, contents = nil, attrs = {}, &block self_closing_tag?(name) ? self_closing_tag(name, attrs) : open_tag(name, attrs) + contents.to_s + closing_tag(name) end |
.normalize_html_attributes(attrs = {}) ⇒ Object
Normalize attrs, replacing boolean keys with their mirrored values.
135 136 137 138 139 140 141 142 143 |
# File 'lib/tagz/tagz.rb', line 135 def normalize_html_attributes attrs = {} return if attrs.blank? attrs.each do |name, value| if boolean_attribute? name value ? attrs[name] = name : attrs.delete(name) end end ' ' + attrs.to_html_attributes end |
.open_tag(name, attrs = {}) ⇒ Object
Return an opening tag of name, with attrs.
127 128 129 |
# File 'lib/tagz/tagz.rb', line 127 def open_tag name, attrs = {} "\n<#{name}#{normalize_html_attributes(attrs)}>" end |
.self_closing_tag(name, attrs = {}) ⇒ Object
Return a self closing tag of name, with attrs.
120 121 122 |
# File 'lib/tagz/tagz.rb', line 120 def self_closing_tag name, attrs = {} "\n<#{name}#{normalize_html_attributes(attrs)}/>" end |
.self_closing_tag?(name) ⇒ Boolean
Check if tag name is a self-closing tag.
113 114 115 |
# File 'lib/tagz/tagz.rb', line 113 def self_closing_tag? name name.in? SELF_CLOSING_TAGS end |
.tag(name, contents = nil, attrs = {}, &block) ⇒ Object
Return markup for tag name. Optionally contents may be passed, which is literal content for spanning tags such as textarea, etc. A hash of attrs may be passed as the second or third argument.
Self closing tags such as <br/>, <input/> etc are automatically closed, and boolean attributes of “selected”, “checked” etc are mirrored or removed when true or false.
Examples
tag :br
# => <br/>
tag :div
# => <div></div>
tag :div, 'hello'
# => <div>hello</div>
tag :div, 'hello', :id => 'comment'
# => <div id="comment">hello</div>
tag :div :id => 'comment'
# => <div id="comment"></div>
tag :div do
tag :p, 'Hello World'
end
# => <div><p>Hello World</p></div>
tag :input, :type => :checkbox, :checked => true
# => <input type="checkbox" checked="checked" />
90 91 92 93 |
# File 'lib/tagz/tagz.rb', line 90 def tag name, contents = nil, attrs = {}, &block attrs, contents = contents, nil if contents.is_a? Hash create_tag name, contents, attrs, &block end |