Dynamic layouts

In markup oriented eno usecases we often deal with sections that contain dynamic layouts of content, meaning the quantity, order and type of elements is variable and we can not just statically query everything by key but instead we iterate and respond to what we find dynamically.

document.elements.each do |element|
  if element.string_key == 'foo'
    # ...
  end
end

The elements accessor returns the ordered elements of a section as an array. As every element can be asked for a key, we can safely use this to determine further actions on an element.

document.elements.each do |element|
  if element.yields_field
    value = element.to_field.required_string_value  
    # ...
  elsif element.yields_list
    # ...
  end
end

Not every element we encounter in a section has a value (fieldsets, lists and sections don't), therefore we use the yields_[element_type] methods to check for a specific element type before we make any queries that require e.g. a field when we expect different types.


Next page: Requiring all elements