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

ESCAPE_VALUES =

Tag values escaped to html entities.

{
  "&" => "&",
  "<" => "&lt;",
  ">" => "&gt;",
  '"' => "&quot;"
}.freeze
ESCAPE_REGEXP =

Cached Regexp for escaping values to avoid rebuilding one on every escape operation.

Regexp.union(*ESCAPE_VALUES.keys).freeze
BOOLEAN_ATTRIBUTES =
[
  :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" />
[
  :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

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.

Examples:

(:p, 'Hello World', :class => 'light')

# => <p class="light">
# =>   Hello World
# => </p>

(:p, :class => 'dark') do
  link_to 'Padrino', 'http://www.padrinorb.com'
end

# => <p class="dark">
# =>   <a href="http://www.padrinorb.com">Padrino</a>
# => </p>

Overloads:

  • #content_tag(name, content, options = nil) ⇒ String

    Parameters:

    • name (Symbol)

      The name of the HTML tag to create.

    • content (String)

      The content inside of the tag.

    • options (Hash) (defaults to: nil)

      The HTML options to include in this tag.

  • #content_tag(name, options = nil, &block) ⇒ String

    Parameters:

    • name (Symbol)

      The name of the HTML tag to create.

    • options (Hash) (defaults to: nil)

      The HTML options to include in this tag.

    • block (Proc)

      The block returning HTML content.

Parameters:

  • options (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the element.

  • :class (String)

    Specifies the stylesheet class of the element.

  • :title (String)

    Specifies the title for the element.

  • :accesskey (String)

    Specifies a shortcut key to access the element.

  • :dropzone (Symbol)

    Specifies what happens when dragged items are dropped on the element. (:copy, :link, :move)

  • :hidden (Boolean)

    Specifies whether or not the element is hidden from view.

  • :draggable (Boolean)

    Specifies whether or not the element is draggable. (true, false, :auto)

  • :contenteditable (Boolean)

    Specifies whether or not the element is editable.

Returns:

  • (String)

    Generated HTML with specified options.



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 119

def (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{ |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

#input_tag(type, options = {}) ⇒ String

Creates an HTML input field with the given type and options.

Examples:

input_tag :text, :name => 'handle'
# => <input type="test" name="handle" />

input_tag :password, :name => 'password', :size => 20
# => <input type="password" name="password" size="20" />

input_tag :text, :name => 'username', :required => true, :autofocus => true
# => <input type="text" name="username" required autofocus />

input_tag :number, :name => 'credit_card', :autocomplete => :off
# => <input type="number" name="credit_card" autocomplete="off" />  

Parameters:

  • type (Symbol)

    The type of input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this input.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the input.

  • :class (String)

    Specifies the stylesheet class of the input.

  • :name (String)

    Specifies the name of the input.

  • :accesskey (String)

    Specifies a shortcut key to access the input.

  • :tabindex (Integer)

    Specifies the tab order of the input.

  • :hidden (Boolean)

    Specifies whether or not the input is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the input should have it's spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the input is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the input's value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the input should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the input should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the input is required to be completed before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the input is read only.

  • :disabled (Boolean)

    Specifies whether or not the input is disabled.

Returns:

  • (String)

    Generated HTML with specified options.



202
203
204
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 202

def input_tag(type, options = {})
  tag(:input, { :type => type }.update(options))
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.

See Also:



145
146
147
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 145

def (name, content = nil, options = nil, &block)
  mark_safe((name, mark_safe(content), options, &block))
end

#tag(name, options = nil, open = false) ⇒ String

Creates an HTML tag with the given name and options.

Examples:

tag :hr, :class => 'dotted'
# => <hr class="dotted" />

tag :input, :name => 'username', :type => :text
# => <input name="username" type="text" />

tag :img, :src => 'images/pony.jpg', :alt => 'My Little Pony'
# => <img src="images/pony.jpg" alt="My Little Pony" />

tag :img, :src => 'sinatra.jpg', :data => { :nsfw => false, :geo => [34.087, -118.407] }
# => <img src="sinatra.jpg" data-nsfw="false" data-geo="34.087 -118.407" />

Parameters:

  • name (Symbol)

    The name of the HTML tag to create.

  • options (Hash) (defaults to: nil)

    The HTML options to include in this tag.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the element.

  • :class (String)

    Specifies the stylesheet class of the element.

  • :title (String)

    Specifies the title for the element.

  • :accesskey (String)

    Specifies a shortcut key to access the element.

  • :dropzone (Symbol)

    Specifies what happens when dragged items are dropped on the element. (:copy, :link, :move)

  • :hidden (Boolean)

    Specifies whether or not the element is hidden from view.

  • :draggable (Boolean)

    Specifies whether or not the element is draggable. (true, false, :auto)

  • :contenteditable (Boolean)

    Specifies whether or not the element is editable.

Returns:

  • (String)

    Generated HTML with specified options.



232
233
234
235
236
# File 'padrino-helpers/lib/padrino-helpers/tag_helpers.rb', line 232

def tag(name, options = nil, open = false)
  options = parse_data_options(name, options)
  attributes = tag_attributes(options)
  "<#{name}#{attributes}#{open ? '>' : ' />'}".html_safe
end