This is outdated documentation for Sycamore.
For up-to-date documentation, see the latest version (v0.8).
view!
Sycamore uses the view!
macro as an ergonomic way to create complex user interfaces. You might
have already seen it in the “Hello, World!” example.
Syntax
Elements
Creating HTML elements is easy as pie with the view!
macro. Since you’ll likely want to create
a lot of elements in your app, there is a special terse syntax.
view!
Text nodes
Of course, in your app, you probably want to display some text. To create a text node, simply add a
string literal (using "
).
view!
Nesting
Creating all these top-level nodes is not very useful. You can create nested nodes like so.
view!
Attributes
Attributes (including classes and ids) can also be specified.
view!
dangerously_set_inner_html
The special dangerously_set_inner_html
attribute is used to set an HTML string as the child of an
element. This should generally be avoided because it is a possible security risk. Never pass user
input to this attribute as that will create an XSS (Cross-Site Scripting) vulnerability.
view!
Instead, when displaying user input, use interpolation syntax instead.
Events
Events are attached using the on:*
directive.
view!
Fragments
As seen in previous examples, views can also be fragments. You can create as many nodes as you want at the top-level.
view!
Fragments can also be empty.
view!
Interpolation
Views can contain interpolated values. Anything that implements std::fmt::Display
will
automatically be inserted as text into the DOM tree. For example:
let my_number = 123;
view!
Other views created using the view!
macro can also be interpolated using the same syntax.
For example:
let inner_view = view! ;
let outer_view = view! ;
The cool thing about interpolation in Sycamore is that it is automatically kept up to date with the value of the expression. Learn more about this in Reactivity.