Expand description
§About Hypersynthetic
Hypersynthetic is a library for writing HTML inside Rust. It is inspired by JSX and HEEx templates, and tries to be different from Tera and Minijinja by enabling Locality of Behavior (LoB) and only allowing reusing HTML code via composition and not via inheritance. It is suitable for building traditional web applications, where backend responds with HTML.
Here is an example of what hypersynthetic can do:
use hypersynthetic::prelude::*;
#[component]
fn TodoItem(text: &str, done: bool) -> HtmlFragment {
let text_decoration = if done { "line-through" } else { "none" };
html! {
<li style="text-decoration: {text_decoration};">
{text}
</li>
}
}
fn main() {
let todo_list = vec![
("Buy Milk", true),
("Read Rust Book", false),
("Write Web App using html! macro", false),
];
let html_list = html! {
<ul>
<TodoItem :for={(text, done) in todo_list} text={text} done={done} />
</ul>
};
// ... Render `html_list` into your application.
assert_eq!(html_list.to_string(), "\
<ul>\
<li style=\"text-decoration: line-through;\">Buy Milk</li>\
<li style=\"text-decoration: none;\">Read Rust Book</li>\
<li style=\"text-decoration: none;\">Write Web App using html! macro</li>\
</ul>"
);
}In this example:
The TodoItem component displays a to-do item, striking it through if it’s done. The main function defines a list of to-dos and uses the :for attribute to loop over them, rendering each one using the TodoItem component.
See the html macro for the description of the syntax and [component] macro for more details about using components
§Features
rocket: Enables integration with the Rocket web framework. It allows to return HtmlFragment from the route handlers and sets the response content type totext/html.axum: Enables integration with the Axum web framework. It allows to return HtmlFragment from the route handlers and sets the response content type totext/html.
Re-exports§
pub use typed_builder;pub use typed_builder_macro;
Modules§
Macros§
- html
- The
htmlmacro allows to construct html fragments in Rust. It mostly follows the same syntax as HTML with the following exceptions:
Structs§
Enums§
Functions§
- escape_
attribute - Escape a string to be used in a quoted attribute.
- escape_
text - Escape a string used in a text node, i.e. regular text.
Attribute Macros§
- component
- The component macro provides a way to define reusable and self-contained web components. A component is a function that returns a HtmlFragment. The easiest way to create one is html macro.