Class: Hanami::Helpers::HtmlHelper::HtmlBuilder
- Inherits:
-
Object
- Object
- Hanami::Helpers::HtmlHelper::HtmlBuilder
- Includes:
- Utils::ClassAttribute
- Defined in:
- lib/hanami/helpers/html_helper/html_builder.rb
Overview
HTML Builder
Direct Known Subclasses
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
"\n".freeze
Instance Method Summary collapse
-
#empty_tag(name, attributes = nil) ⇒ self
Defines a custom empty tag.
-
#encode(encoding) ⇒ String
private
Encode the content with the given character encoding.
-
#fragment(&blk) ⇒ self
Define a HTML fragment.
-
#initialize ⇒ Hanami::Helpers::HtmlHelper::HtmlBuilder
constructor
private
Initialize a new builder.
-
#method_missing(method_name, *args, &blk) ⇒ Object
private
Forward missing methods to the current context.
-
#nested? ⇒ TrueClass, FalseClass
private
Check if there are nested nodes.
- #options ⇒ Object private
-
#resolve(&blk) ⇒ Object
private
Resolve the context for nested contents.
- #respond_to_missing?(method_name, include_all) ⇒ Boolean private
-
#tag(name, content = nil, attributes = nil, &blk) ⇒ self
Define a custom tag.
-
#text(content) ⇒ self
(also: #+)
Defines a plain string of text.
-
#to_s ⇒ Hanami::Utils::Escape::SafeString
private
Resolves all the nodes and generates the markup.
Constructor Details
#initialize ⇒ Hanami::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
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.
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
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
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
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
353 354 355 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 353 def nested? @nodes.any? end |
#options ⇒ 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.
191 192 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 191 def 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
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.
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
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, ) 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.
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_s ⇒ Hanami::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
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 |