The html! macro can reach the default recursion limit of the compiler. If you encounter compilation errors, add an attribute like #![recursion_limit="1024"] in the crate root to overcome the problem.
HTML
The html! macro allows you to write HTML and SVG code declaratively. It is similar to JSX (an extension to JavaScript that allows you to write HTML-like code inside of JavaScript).
Important notes
- The
html!macro only accepts one root html node (you can counteract this by using fragments or iterators) - An empty
html! {}invocation is valid and will not render anything - Literals must always be quoted and wrapped in braces:
html! { <p>{ "Hello, World" }</p> } - The
html!macro will make all tag names lowercase. To use upper case characters (which are required for some SVG elements) use dynamic tag names:html! { <@{"myTag"}></@> }
Tag Structure#
Tags are based on HTML tags. Components, Elements, and Lists are all based on this tag syntax.
Tags must either self-close <... /> or have a corresponding end tag for each start tag.
- Open - Close
- Invalid
use *;
html! ;
use *;
html! ;
- Self-closing
- Invalid
use *;
html! ;
use *;
html! ;
For convenience, elements which usually require a closing tag are allowed to self-close. For example, writing html! { <div class="placeholder" /> } is valid.
Children#
Create complex nested HTML and SVG layouts with ease:
- HTML
- SVG
use *;
html! ;
use *;
html! ;
Lints#
If you compile Yew using a nightly version of the Rust compiler, the macro will warn you about some common pitfalls that you might run into. Of course, you may need to use the stable compiler (e.g. your organization might have a policy mandating it) for release builds, but even if you're using a stable toolchain, running cargo +nightly check might flag some ways that you could improve your HTML code.
At the moment the lints are mostly accessibility-related. If you have ideas for lints, please feel free to chime in on this issue.
Specifying attributes and properties#
Attributes are set on elements in the same way as in normal HTML:
use *;
let value = "something";
html! ;
Properties are specified with ~ before the element name:
use *;
html! ;
The braces around the value can be omitted if the value is a literal.
Literals are all valid literal expressions in Rust. Note that negative numbers are not literals and thus must be enclosed in curly-braces {-6}
Component properties are passed as Rust objects and are different from the element attributes/properties described here. Read more about them at Component Properties
Special properties#
There are special properties which don't directly influence the DOM but instead act as instructions to Yew's virtual DOM. Currently, there are two such special props: ref and key.
ref allows you to access and manipulate the underlying DOM node directly. See Refs for more details.
key on the other hand gives an element a unique identifier which Yew can use for optimization purposes.
Read more at Lists
Comments#
It is also possible to use Rust style comments as part of the HTML structure:
use *;
html! ;
Comments will be dropped during the parsing process and will not end up in the final output.
Conditional Rendering#
Markup can be rendered conditionally by using Rust's conditional structures. Currently only if and if let are supported.
use *;
html! ;
Read more at Conditional Rendering