Class: Hanami::Helpers::HtmlHelper::HtmlBuilder

Inherits:
Object
  • Object
show all
Includes:
Utils::ClassAttribute
Defined in:
lib/hanami/helpers/html_helper/html_builder.rb

Overview

HTML Builder

Since:

  • 0.1.0

Direct Known Subclasses

FormHelper::FormBuilder

Constant Summary collapse

CONTENT_TAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTML5 content tags

%w[
  a
  abbr
  address
  article
  aside
  audio
  b
  bdi
  bdo
  blockquote
  body
  button
  canvas
  caption
  cite
  code
  colgroup
  data
  datalist
  del
  details
  dialog
  dfn
  div
  dl
  dt
  dd
  em
  fieldset
  figcaption
  figure
  footer
  form
  h1
  h2
  h3
  h4
  h5
  h6
  head
  header
  hgroup
  i
  iframe
  ins
  kbd
  label
  legend
  li
  main
  map
  mark
  math
  menu
  meter
  nav
  noscript
  object
  ol
  optgroup
  option
  output
  p
  pre
  progress
  q
  rp
  rt
  rtc
  ruby
  s
  samp
  script
  section
  select
  slot
  small
  span
  strong
  style
  sub
  summary
  sup
  svg
  table
  tbody
  td
  template
  textarea
  tfoot
  th
  thead
  time
  title
  tr
  u
  ul
  var
  video
].freeze
EMPTY_TAGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTML5 empty tags

%w[
  area
  base
  br
  col
  embed
  hr
  img
  input
  keygen
  link
  menuitem
  meta
  param
  source
  track
  wbr
].freeze
NEWLINE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

New line separator

Since:

  • 0.1.0

"\n".freeze

Instance Method Summary collapse

Constructor Details

#initializeHanami::Helpers::HtmlHelper::HtmlBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new builder

Since:

  • 0.1.0



186
187
188
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 186

def initialize
  @nodes = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Forward missing methods to the current context. This allows to access views local variables from nested content blocks.

Since:

  • 0.1.0



382
383
384
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 382

def method_missing(method_name, *args, &blk) # rubocop:disable Style/MethodMissingSuper
  @context.__send__(method_name, *args, &blk)
end

Instance Method Details

#empty_tag(name, attributes = nil) ⇒ self

Defines a custom empty tag

Examples:

html.empty_tag(:xr) # => <xr>

html.empty_tag(:xr, id: 'foo') # => <xr id="foo">

html.empty_tag(:xr, id: 'foo', 'data-xyz': 'bar') # => <xr id="foo" data-xyz="bar">

Parameters:

  • name (Symbol, String)

    the name of the tag

  • attributes (Hash, NilClass) (defaults to: nil)

    the optional tag attributes

Returns:

  • (self)

See Also:

Since:

  • 0.1.0



285
286
287
288
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 285

def empty_tag(name, attributes = nil)
  @nodes << EmptyHtmlNode.new(name, attributes)
  self
end

#encode(encoding) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encode the content with the given character encoding

Parameters:

  • encoding (Encoding, String)

    the encoding or its string representation

Returns:

  • (String)

    the encoded string

Since:

  • 0.2.5



343
344
345
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 343

def encode(encoding)
  to_s.encode(encoding)
end

#fragment(&blk) ⇒ self

Define a HTML fragment

Examples:

html.fragment('Hanami') # => Hanami

html do
  p 'hello'
  p 'hanami'
end
# =>
  <p>hello</p>
  <p>hanami</p>

Parameters:

  • blk (Proc)

    the optional nested content espressed as a block

Returns:

  • (self)

See Also:

Since:

  • 0.2.6



262
263
264
265
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 262

def fragment(&blk)
  @nodes << HtmlFragment.new(&blk)
  self
end

#nested?TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if there are nested nodes

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



353
354
355
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 353

def nested?
  @nodes.any?
end

#optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



191
192
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 191

def options
end

#resolve(&blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve the context for nested contents

Since:

  • 0.1.0



362
363
364
365
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 362

def resolve(&blk)
  @context = blk.binding.receiver
  instance_exec(&blk)
end

#respond_to_missing?(method_name, include_all) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 1.2.2



388
389
390
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 388

def respond_to_missing?(method_name, include_all)
  @context.respond_to?(method_name, include_all)
end

#tag(name, content = nil, attributes = nil, &blk) ⇒ self

Define a custom tag

Examples:

html.tag(:custom) # => <custom></custom>

html.tag(:custom, 'foo') # => <custom>foo</custom>

html.tag(:custom, html.p('hello')) # => <custom><p>hello</p></custom>

html.tag(:custom) { 'foo' }
# =>
#<custom>
#  foo
#</custom>

html.tag(:custom) do
  p 'hello'
end
# =>
#<custom>
#  <p>hello</p>
#</custom>

html.tag(:custom, 'hello', id: 'foo', 'data-xyz': 'bar') # => <custom id="foo" data-xyz="bar">hello</custom>

html.tag(:custom, id: 'foo') { 'hello' }
# =>
#<custom id="foo">
#  hello
#</custom>

Parameters:

  • name (Symbol, String)

    the name of the tag

  • content (String, Hanami::Helpers::HtmlHelper::HtmlBuilder, NilClass) (defaults to: nil)

    the optional content

  • attributes (Hash, NilClass) (defaults to: nil)

    the optional tag attributes

  • blk (Proc)

    the optional nested content espressed as a block

Returns:

  • (self)

See Also:

Since:

  • 0.1.0



236
237
238
239
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 236

def tag(name, content = nil, attributes = nil, &blk)
  @nodes << HtmlNode.new(name, blk || content, attributes || content, options)
  self
end

#text(content) ⇒ self Also known as: +

Defines a plain string of text. This particularly useful when you want to build more complex HTML.

Examples:


<%=
  html.label do
    text "Option 1"
    radio_button :option, 1
  end
%>

<!-- output -->
<label>
  Option 1
  <input type="radio" name="option" value="1" />
</label>

Parameters:

  • content (String)

    the text to be rendered.

Returns:

  • (self)

See Also:

Since:

  • 0.1.0



314
315
316
317
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 314

def text(content)
  @nodes << TextNode.new(content)
  self
end

#to_sHanami::Utils::Escape::SafeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves all the nodes and generates the markup

Returns:

  • (Hanami::Utils::Escape::SafeString)

    the output

See Also:

Since:

  • 0.1.0



331
332
333
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 331

def to_s
  Utils::Escape::SafeString.new(@nodes.map(&:to_s).join(NEWLINE))
end