Class: ListComponent

Inherits:
Object
  • Object
show all
Defined in:
lib/html-native/collections.rb

Overview

ListComponent represents an HTML list based on an Enumerable collection.

Attributes in an ListComponent 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 an ordered list with 20px padding and the class of “list-item” given to each item, you could write: “‘ ListComponent.new(data, ordered: true, attributes:

{list: {style: "padding: 20px"}, item: {class: "list-item"}})

“‘ which is equivalent to “` <ol style=“padding: 20px”>

<li class="list-item">...</li>
<li class="list-item">...</li>
...

</ol> “‘

Instance Method Summary collapse

Constructor Details

#initialize(data, attributes: {}, ordered: false, &block) ⇒ ListComponent

Creates a new instance of ListComponent from the values of data.

This list can be either ordered or unordered, depending on ordered parameter. If ordered is true, the list will be ordered, otherwise it will be unordered. ordered is false by default.

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.



174
175
176
177
# File 'lib/html-native/collections.rb', line 174

def initialize(data, attributes: {}, ordered: false, &block)
  @list = ordered ? OrderedListComponent.new(data, attributes, &block) : 
                    UnorderedListComponent.new(data, attributes, &block)
end

Instance Method Details

#renderObject

Converts the ListComponent 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.



184
185
186
# File 'lib/html-native/collections.rb', line 184

def render
  @list.render
end