Module: Pagescript::TagsHelper

Defined in:
lib/pagescript/helpers/tags_helper.rb

Overview

A helper that creates elements with metadata attached as data attributes.

Instance Method Summary collapse

Instance Method Details

#body_tag(params_as_metadata: false, **kwargs) { ... } ⇒ String

Generates a ‘<body>` tag with HTML5 data attributes for the controller & action name. The keyword arguments are forwarded to tag and content_tag.

Examples:

when called without a block

body_tag
=>  <body data-controller="foo" data-action="bar">

when called with a block

body_tag { 'hello world' }
=>  <body data-controller="foo" data-action="bar">Hello World</body>

when called with keyword arguments

body_tag(foo: 'bar', data: { x: 2 })
=>  <body data-controller="foo" data-action="bar" foo="" data-x="2">

when params_as_metadata is true

body_tag(params_as_metadata: true)
=>  <body data-controller="foo" data-action="bar" data-params-user-id="2">

Parameters:

  • params_as_metadata (Boolean) (defaults to: false)

    default: false. Includes params from the URL as data attributes. The data attributes are prefixed with param-. Underscores in the param name are converted to ‘-’.

Yields:

  • Yields flow to an optional block. Using a block adds a closing </body> tag.

Returns:

  • (String)

See Also:

Since:

  • 0.1.0



29
30
31
32
33
34
35
36
# File 'lib/pagescript/helpers/tags_helper.rb', line 29

def body_tag(params_as_metadata: false, **kwargs)
  options = kwargs.deep_merge( data: data_attrs() )
  if block_given?
    (:body, options) { yield }
  else
    tag(:body, options, true)
  end
end