module Padrino::Helpers::TagHelpers
def content_tag(name, content = nil, options = nil, &block)
-
(String)
-
Options Hash:
(**options)
-
:contenteditable
(Boolean
) -- -
:draggable
(Boolean
) -- -
:hidden
(Boolean
) -- -
:dropzone
(Symbol
) -- -
:accesskey
(String
) -- -
:title
(String
) -- -
:class
(String
) -- -
:id
(String
) --
Parameters:
-
block
(Proc
) -- -
options
(Hash
) -- -
name
(Symbol
) -- -
options
(Hash
) -- -
content
(String
) -- -
name
(Symbol
) --
Overloads:
-
content_tag(name, options = nil, &block)
-
content_tag(name, content, options = nil)
def content_tag(name, content = nil, options = nil, &block) if block_given? options = content if content.is_a?(Hash) content = capture_html(&block) end options = parse_data_options(name, options) attributes = tag_attributes(options) output = SafeBuffer.new output.safe_concat "<#{name}#{attributes}>" if content.respond_to?(:each) && !content.is_a?(String) content.each{ |item| output.concat item; output.safe_concat NEWLINE } else output.concat content.to_s end output.safe_concat "</#{name}>" block_is_template?(block) ? concat_content(output) : output end
def escape_link(link)
# => 'already%20partially%20escaped'
escape_link('already%20partially escaped')
# => 'http://example.com/spaced%20link'
escape_link('http://example.com/spaced link')
@example
Returns an escaped document link.
#
def escape_link(link) link.gsub(' ', '%20') end
def escape_value(string)
Escape tag values to their HTML/XML entities.
#
def escape_value(string) string = string.collect(&:to_s).join(' ') if string.is_a?(Array) string.to_s.gsub(ESCAPE_REGEXP, ESCAPE_VALUES) end
def input_tag(type, options = {})
-
(String)
-
Options Hash:
(**options)
-
:disabled
(Boolean
) -- -
:readonly
(Boolean
) -- -
:required
(Boolean
) -- -
:autofocus
(Boolean
) -- -
:autocomplete
(Symbol
) -- -
:pattern
(String
) -- -
:draggable
(Boolean
) -- -
:spellcheck
(Boolean
) -- -
:hidden
(Boolean
) -- -
:tabindex
(Integer
) -- -
:accesskey
(String
) -- -
:name
(String
) -- -
:class
(String
) -- -
:id
(String
) --
Parameters:
-
options
(Hash
) -- -
type
(Symbol
) --
def input_tag(type, options = {}) tag(:input, { :type => type }.update(options)) end
def nested_values(attribute, hash)
Iterate through nested values.
#
def nested_values(attribute, hash) hash.inject('') do |all,(key,value)| attribute_with_name = "#{attribute}-#{key.to_s.tr('_', '-')}" all << if value.is_a?(Hash) nested_values(attribute_with_name, value) else %(#{attribute_with_name}="#{escape_value(value)}" ) end end end
def parse_data_options(tag, options)
Parses custom data attributes.
#
def parse_data_options(tag, options) return unless options parsed_options = options.dup options.each do |key, value| next if !DATA_ATTRIBUTES.include?(key) || (tag.to_s == 'form' && key == :method) parsed_options["data-#{key}"] = parsed_options.delete(key) parsed_options[:rel] = 'nofollow' if key == :method end parsed_options end
def safe_content_tag(name, content = nil, options = nil, &block)
- See: #content_tag -
def safe_content_tag(name, content = nil, options = nil, &block) mark_safe(content_tag(name, mark_safe(content), options, &block)) end
def tag(name, options = nil, open = false)
-
(String)
-
Parameters:
-
options
(Hash
) -- -
name
(Symbol
) --
def tag(name, options = nil, open = false) options = parse_data_options(name, options) attributes = tag_attributes(options) "<#{name}#{attributes}#{open ? '>' : ' />'}".html_safe end
def tag_attributes(options)
Returns a compiled list of HTML attributes.
#
def tag_attributes(options) return '' unless options options.inject('') do |all,(key,value)| next all unless value all << ' ' if all.empty? all << if value.is_a?(Hash) nested_values(key, value) elsif BOOLEAN_ATTRIBUTES.include?(key) %(#{key}="#{key}" ) else %(#{key}="#{escape_value(value)}" ) end end.chomp!(' ') end