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
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
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 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
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
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
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
@api private
# File lib/hanami/helpers/html_helper/html_builder.rb, line 191 def options end
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
@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
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
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
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