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 dfn div dl dt dd em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header i iframe ins kbd label legend li link main map mark math menu meter nav noscript object ol optgroup option output p pre progress q rp rt ruby s samp script section select small span strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr u ul 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(m, *args, &blk) ⇒ Object
private
Forward missing methods to the current context.
-
#nested? ⇒ TrueClass, FalseClass
private
Check if there are nested nodes.
- #options ⇒ Object
- #resolve(&blk) ⇒ Object
-
#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
182 183 184 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 182 def initialize @nodes = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *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.
370 371 372 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 370 def method_missing(m, *args, &blk) @context.__send__(m, *args, &blk) end |
Instance Method Details
#empty_tag(name, attributes = nil) ⇒ self
Defines a custom empty tag
280 281 282 283 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 280 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
335 336 337 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 335 def encode(encoding) to_s.encode(encoding) end |
#fragment(&blk) ⇒ self
Define a HTML fragment
257 258 259 260 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 257 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
345 346 347 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 345 def nested? @nodes.any? end |
#options ⇒ Object
186 187 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 186 def end |
#resolve(&blk) ⇒ Object
354 355 356 357 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 354 def resolve(&blk) @context = blk.binding.receiver instance_exec(&blk) end |
#tag(name, content = nil, attributes = nil, &blk) ⇒ self
Define a custom tag
231 232 233 234 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 231 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.
306 307 308 309 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 306 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
323 324 325 |
# File 'lib/hanami/helpers/html_helper/html_builder.rb', line 323 def to_s Utils::Escape::SafeString.new(@nodes.map(&:to_s).join(NEWLINE)) end |