class Hanami::Helpers::HtmlHelper::HtmlBuilder

HTML Builder

@since 0.1.0

Constants

CONTENT_TAGS

HTML5 content tags

@since 0.1.0 @api private

@see Hanami::Helpers::HtmlHelper::HtmlNode @see developer.mozilla.org/en-US/docs/Web/HTML/Element

EMPTY_TAGS

HTML5 empty tags

@since 0.1.0 @api private

@see Hanami::Helpers::HtmlHelper::EmptyHtmlNode @see developer.mozilla.org/en-US/docs/Web/HTML/Element

NEWLINE

New line separator

@since 0.1.0 @api private

Public Class Methods

new() click to toggle source

Initialize a new builder

@return [Hanami::Helpers::HtmlHelper::HtmlBuilder] the builder

@since 0.1.0 @api private

# File lib/hanami/helpers/html_helper/html_builder.rb, line 186
def initialize
  @nodes = []
end

Public Instance Methods

+(content)

@since 0.2.5 @api private

Alias for: text
empty_tag(name, attributes = nil) click to toggle source

Defines a custom empty tag

@param name [Symbol,String] the name of the tag @param attributes [Hash,NilClass] the optional tag attributes

@return [self]

@since 0.1.0 @api public

@see Hanami::Helpers::HtmlHelper

@example

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">
# 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) click to toggle source

Encode the content with the given character encoding

@param encoding [Encoding,String] the encoding or its string representation

@return [String] the encoded string

@since 0.2.5 @api private

# File lib/hanami/helpers/html_helper/html_builder.rb, line 343
def encode(encoding)
  to_s.encode(encoding)
end
fragment(&blk) click to toggle source

Define a HTML fragment

@param blk [Proc] the optional nested content espressed as a block

@return [self]

@since 0.2.6 @api public

@see Hanami::Helpers::HtmlHelper

@example

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

html do
  p 'hello'
  p 'hanami'
end
# =>
  <p>hello</p>
  <p>hanami</p>
# File lib/hanami/helpers/html_helper/html_builder.rb, line 262
def fragment(&blk)
  @nodes << HtmlFragment.new(&blk)
  self
end
method_missing(method_name, *args, &blk) click to toggle source

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

@since 0.1.0 @api private

# 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
nested?() click to toggle source

Check if there are nested nodes

@return [TrueClass,FalseClass] the result of the check

@since 0.1.0 @api private

# File lib/hanami/helpers/html_helper/html_builder.rb, line 353
def nested?
  @nodes.any?
end
options() click to toggle source

@api private

# File lib/hanami/helpers/html_helper/html_builder.rb, line 191
def options
end
resolve(&blk) click to toggle source

Resolve the context for nested contents

@since 0.1.0 @api private

# 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) click to toggle source

@since 1.2.2 @api private

# 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) click to toggle source

Define a custom tag

@param name [Symbol,String] the name of the tag @param content [String,Hanami::Helpers::HtmlHelper::HtmlBuilder,NilClass] the optional content @param attributes [Hash,NilClass] the optional tag attributes @param blk [Proc] the optional nested content espressed as a block

@return [self]

@since 0.1.0 @api public

@see Hanami::Helpers::HtmlHelper

@example

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>
# 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) click to toggle source

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

@param content [String] the text to be rendered.

@return [self]

@see Hanami::Helpers::HtmlHelper @see Hanami::Helpers::HtmlHelper::TextNode

@example

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

<!-- output -->
<label>
  Option 1
  <input type="radio" name="option" value="1" />
</label>
# File lib/hanami/helpers/html_helper/html_builder.rb, line 314
def text(content)
  @nodes << TextNode.new(content)
  self
end
Also aliased as: +
to_s() click to toggle source

Resolves all the nodes and generates the markup

@return [Hanami::Utils::Escape::SafeString] the output

@since 0.1.0 @api private

@see www.rubydoc.info/gems/hanami-utils/Hanami/Utils/Escape/SafeString

# 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