Module: Flatrack::View::TagHelper

Includes:
ERB::Util, CaptureHelper
Included in:
Flatrack::View, LinkHelper
Defined in:
lib/flatrack/view/tag_helper.rb

Overview

View helpers for rendering various html tags

Constant Summary collapse

PRE_CONTENT_STRINGS =
{
    textarea: "\n"
}
BOOLEAN_ATTRIBUTES =
%w(disabled readonly multiple checked autobuffer
autoplay controls loop selected hidden scoped
async defer reversed ismap seamless muted
required autofocus novalidate formnovalidate open
pubdate itemscope allowfullscreen default inert
sortable truespeed typemustmatch).to_set

Instance Method Summary collapse

Instance Method Details

#html_tag(name, content, options = {}) ⇒ String #html_tag(name, options = {}) { ... } ⇒ String

Creates an HTML tag

Overloads:

  • #html_tag(name, content, options = {}) ⇒ String

    Creates an html tag using the provided content as the content of the tag.

    Parameters:

    • name (String)

      the name of the tag (i.e. a, img, style)

    • content (String)

      the content of the tag

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

      the html options for the tag

    Returns:

    • (String)
  • #html_tag(name, options = {}) { ... } ⇒ String

    Creates an html tag using the provided content as the content of the tag.

    Parameters:

    • name (String)

      the name of the tag (i.e. a, img, style)

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

      the html options for the tag

    Yields:

    • the tag content

    Returns:

    • (String)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/flatrack/view/tag_helper.rb', line 40

def html_tag(name, *args, &block)
  content, options, escape = args
  if block_given?
    check_arguments [name, *args], 1..3
    options, escape = content, options
    content = capture(&block)
  else
    check_arguments [name, *args], 2..4
  end
  escape = true if escape.nil?
  html_tag_string(name, content, options, escape)
end

#image_tag(uri, options = {}) ⇒ String

Returns an HTML image tag

Parameters:

  • uri (String)

    location of the image

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

    the html options for the tag

Returns:

  • (String)


57
58
59
60
61
# File 'lib/flatrack/view/tag_helper.rb', line 57

def image_tag(uri, options = {})
  uri = asset_path(uri) unless uri =~ %r{^(http)?(s)?:?\/\/}
  options.merge! src: uri
  html_tag(:img, nil, options)
end

#javascript_tag(uri) ⇒ String

Returns an HTML script tag for javascript

Parameters:

  • uri (String)

    location of the javascript file

Returns:

  • (String)


66
67
68
69
# File 'lib/flatrack/view/tag_helper.rb', line 66

def javascript_tag(uri)
  uri = asset_path(uri) + '.js' if uri.is_a? Symbol
  html_tag(:script, '', src: uri, type: 'application/javascript')
end

#stylesheet_tag(uri) ⇒ String

Returns an HTML link tag for css

Parameters:

  • uri (String)

    location of the css file

Returns:

  • (String)


74
75
76
77
# File 'lib/flatrack/view/tag_helper.rb', line 74

def stylesheet_tag(uri)
  uri = asset_path(uri) + '.css' if uri.is_a? Symbol
  html_tag(:link, nil, rel: 'stylesheet', type: 'text/css', href: uri)
end