Lists

Fragments

The html! macro always requires a single root node. In order to get around this restriction, it’s valid to wrap content in empty tags:

  • Valid
  • Invalid
  1. use yew::html;
  2. html! {
  3. <>
  4. <div></div>
  5. <p></p>
  6. </>
  7. }
  1. use yew::html;
  2. /* error: only one root html element allowed */
  3. html! {
  4. <div></div>
  5. <p></p>
  6. }

Iterators

Yew supports two different syntaxes for building html from an iterator:

  • Syntax type 1
  • Syntax type 2
  1. use yew::{html, Html};
  2. html! {
  3. <ul class="item-list">
  4. { self.props.items.iter().map(renderItem).collect::<Html>() }
  5. </ul>
  6. }
  1. use yew::html;
  2. html! {
  3. <ul class="item-list">
  4. { for self.props.items.iter().map(renderItem) }
  5. </ul>
  6. }

Relevant examples