HTML with html!
You can write expressions resembling HTML with the html! macro. Behind the scenes, Yew turns it into rust code representing the DOM to generate.
use *;
let my_header: Html = html! ;
Similar to format expressions, there is an easy way to embed values from the surrounding context into the HTML by applying curly brackets:
use *;
let header_text = "Hello world".to_string;
let header_html: Html = html! ;
let count: usize = 5;
let counter_html: Html = html! ;
let combined_html: Html = html! ;
One major rule comes with the use of html! - you can only return 1 wrapping node. To render a list of multiple elements, html! allows fragments. Fragments are tags without a name, that produce no HTML element by themselves.
- Invalid
- Valid
use html;
// error: only one root HTML element allowed
html! ;
use html;
// fixed: using HTML fragments
html! ;
We will introduce Yew and HTML further in depth in more HTML.