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.

const elements = document.elements();

elements.forEach(element => {
  if(element.stringKey() === 'foo') {
    // ...
  }
});

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.

const elements = document.elements();

elements.forEach(element => {
  if(element.yieldsField()) {
    const value = element.toField().requiredStringValue();
    
    // ...
  } else if(element.yieldsList()) {
    // ...
  }
});

Not every element we encounter in a section has a value (fieldsets, lists and sections don't), therefore we use the yields\[ElementType]() 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