Class: UnorderedListComponent
- Inherits:
-
Object
- Object
- UnorderedListComponent
- Includes:
- HTMLComponent
- Defined in:
- lib/html-native/collections.rb
Overview
UnorderedListComponent represents an HTML unordered list generated from an Enumerable collection.
Attributes in an UnorderedListComponent are separated into multiple groups since there are multiple kinds of tags. These groups are:
-
list - The attributes associated with the <ul> element
-
item - The attributes associated with <li> elements
For example, to have a list with 20px padding and the class of “list-item” given to each item, you could write: “‘ UnorderedListComponent.new(data, attributes:
{list: {style: "padding: 20px"}, item: {class: "list-item"}})
“‘ which is equivalent to “` <ul style=“padding: 20px”>
<li class="list-item">...</li>
<li class="list-item">...</li>
...
</ul> “‘
Constant Summary
Constants included from HTMLComponent
HTMLComponent::FORBIDDEN_ATTRIBUTES, HTMLComponent::LIMITED_ATTRIBUTES, HTMLComponent::TAG_LIST
Instance Method Summary collapse
-
#initialize(data, attributes: {}, &block) ⇒ UnorderedListComponent
constructor
Creates a new instance of UnorderedListComponent from the values of data.
-
#render ⇒ Object
Converts the UnorderedListComponent instance to the equivalent HTML.
Methods included from HTMLComponent
#_if, #_label, #_unless, #doctype, singleton, #valid_attribute?
Constructor Details
#initialize(data, attributes: {}, &block) ⇒ UnorderedListComponent
Creates a new instance of UnorderedListComponent from the values of data.
If a block is given, each item in data is passed to it to render the list items. If no block is given, data are used directly.
120 121 122 123 124 125 |
# File 'lib/html-native/collections.rb', line 120 def initialize(data, attributes: {}, &block) @list_data = data @list_attributes = attributes[:list] || {} @item_attributes = attributes[:item] || {} @block = block end |
Instance Method Details
#render ⇒ Object
Converts the UnorderedListComponent instance to the equivalent HTML.
render can be called directly, but that usually isn’t necessary. HTMLComponent::Builder handles this automatically, so it only needs to be done if there is no prior instance of one.
132 133 134 135 136 137 138 139 140 |
# File 'lib/html-native/collections.rb', line 132 def render ul(@list_attributes) do @list_data.component_map do |l| li(@item_attributes) do @block ? @block.call(l) : l end end end end |