module Padrino::Helpers::TagHelpers
def content_tag(name, content = nil, options = nil, &block)
- Api: - public
Returns:
-
(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 = ActiveSupport::SafeBuffer.new output.safe_concat "<#{name}#{attributes}>" if content.respond_to?(:each) && !content.is_a?(String) content.each { |c| output.concat c; output.safe_concat NEWLINE } else output.concat content end output.safe_concat "</#{name}>" block_is_template?(block) ? concat_content(output) : output end
def escape_value(string)
Escape tag values to their HTML/XML entities.
#
def escape_value(string) string.to_s.gsub(ESCAPE_REGEXP) { |c| ESCAPE_VALUES[c] } end
def input_tag(type, options = {})
- Api: - semipublic
Returns:
-
(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, options.reverse_merge!(:type => type)) end
def nested_values(attribute, hash)
Iterate through nested values
#
def nested_values(attribute, hash) hash.map do |k, v| if v.is_a?(Hash) nested_values("#{attribute}-#{k.to_s.dasherize}", v) else %(#{attribute}-#{k.to_s.dasherize}="#{escape_value(v)}") end end * ' ' end
def parse_data_options(tag, options)
Parses custom data attributes
#
def parse_data_options(tag, options) return if options.nil? parsed_options = options.dup options.each do |k, v| next if !DATA_ATTRIBUTES.include?(k) || (tag.to_s == 'form' && k == :method) parsed_options["data-#{k}"] = parsed_options.delete(k) parsed_options[:rel] = 'nofollow' if k == :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)
- Api: - public
Returns:
-
(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 '' if options.nil? attributes = options.map do |k, v| next if v.nil? || v == false if v.is_a?(Hash) nested_values(k, v) elsif BOOLEAN_ATTRIBUTES.include?(k) %(#{k}="#{k}") else %(#{k}="#{escape_value(v)}") end end.compact attributes.empty? ? '' : " #{attributes * ' '}" end