Module: Tagz
- Defined in:
- lib/tagz/tagz.rb,
lib/tagz.rb,
lib/tagz/helpers.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.1.2'
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 = {}) ⇒ 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.
159 160 161 |
# File 'lib/tagz/tagz.rb', line 159 def boolean_attribute? name name.in? BOOLEAN_ATTRIBUTES end |
.closing_tag(name) ⇒ Object
Return closing tag of name.
201 202 203 |
# File 'lib/tagz/tagz.rb', line 201 def closing_tag name "</#{name}>" end |
.create_tag(name, contents = nil, attrs = {}) ⇒ Object
:stopdoc:
150 151 152 153 154 |
# File 'lib/tagz/tagz.rb', line 150 def create_tag name, contents = nil, attrs = {} 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.
188 189 190 191 192 193 194 195 196 |
# File 'lib/tagz/tagz.rb', line 188 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.
180 181 182 |
# File 'lib/tagz/tagz.rb', line 180 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.
173 174 175 |
# File 'lib/tagz/tagz.rb', line 173 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.
166 167 168 |
# File 'lib/tagz/tagz.rb', line 166 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 :div do |div|
div.tag :p, 'Hello World'
end
# => <div><p>Hello World</p></div>
tag :input, :type => :checkbox, :checked => true
# => <input type="checkbox" checked="checked" />
143 144 145 146 |
# File 'lib/tagz/tagz.rb', line 143 def tag name, contents = nil, attrs = {}, &block attrs, contents = contents, nil if contents.is_a? Hash Tag.new(name, contents, attrs, &block).to_s end |