Module: Padrino::Helpers::TagHelpers
- Defined in:
- padrino-helpers/lib/padrino-helpers/tag_helpers.rb
Overview
Helpers related to producing html tags within templates.
Constant Summary collapse
- ESCAPE_VALUES =
Tag values escaped to html entities.
{ '&' => '&', '<' => '<', '>' => '>', '"' => '"' }.freeze
- ESCAPE_REGEXP =
Cached Regexp for escaping values to avoid rebuilding one on every escape operation.
Regexp.union(*ESCAPE_VALUES.keys).freeze
- BOOLEAN_ATTRIBUTES =
%i[ autoplay autofocus formnovalidate checked disabled hidden loop multiple muted readonly required selected declare defer ismap itemscope noresize novalidate ].freeze
- DATA_ATTRIBUTES =
Custom data attributes, feel free to update with yours:
Padrino::Helpers::TagHelpers::DATA_ATTRIBUTES.push(:dialog) text_field :foo, dialog: true # Generates: <input type="text" data-dialog="true" name="foo" /> %i[method remote confirm]
- NEWLINE =
A html_safe newline string to avoid allocating a new on each concatenation.
"\n".html_safe.freeze
Instance Method Summary collapse
-
#content_tag(name, content = nil, options = nil, &block) ⇒ String
Creates an HTML tag with given name, content, and options.
-
#escape_link(link) ⇒ Object
Returns an escaped document link.
-
#input_tag(type, options = {}) ⇒ String
Creates an HTML input field with the given type and options.
-
#safe_content_tag(name, content = nil, options = nil, &block) ⇒ Object
Like #content_tag, but assumes its input to be safe and doesn’t escape.
-
#tag(name, options = nil, open = false) ⇒ String
Creates an HTML tag with the given name and options.
Instance Method Details
#content_tag(name, content, options = nil) ⇒ String #content_tag(name, options = nil, &block) ⇒ String
Creates an HTML tag with given name, content, and options.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 115 def content_tag(name, content = nil, = nil, &block) if block_given? = content if content.is_a?(Hash) content = capture_html(&block) end = (name, ) attributes = tag_attributes() output = SafeBuffer.new output.safe_concat "<#{name}#{attributes}>" if content.respond_to?(:each) && !content.is_a?(String) content.each do |item| output.concat item output.safe_concat NEWLINE end else output.concat content.to_s end output.safe_concat "</#{name}>" block_is_template?(block) ? concat_content(output) : output end |
#escape_link(link) ⇒ Object
Returns an escaped document link.
247 248 249 |
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 247 def escape_link(link) link.gsub(' ', '%20') end |
#input_tag(type, options = {}) ⇒ String
Creates an HTML input field with the given type and options.
202 203 204 |
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 202 def input_tag(type, = {}) tag(:input, { type: type }.update()) end |
#safe_content_tag(name, content = nil, options = nil, &block) ⇒ Object
Like #content_tag, but assumes its input to be safe and doesn’t escape. It also returns safe HTML.
145 146 147 |
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 145 def safe_content_tag(name, content = nil, = nil, &block) mark_safe(content_tag(name, mark_safe(content), , &block)) end |
#tag(name, options = nil, open = false) ⇒ String
Creates an HTML tag with the given name and options.
232 233 234 235 236 |
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 232 def tag(name, = nil, open = false) = (name, ) attributes = tag_attributes() "<#{name}#{attributes}#{open ? '>' : ' />'}".html_safe end |