14 releases (7 breaking)

0.8.0 Nov 11, 2024
0.6.1 Oct 25, 2024
0.4.0 Jun 13, 2024
0.3.0 Oct 18, 2023

#69 in Template engine

Download history 83/week @ 2024-08-30 46/week @ 2024-09-06 13/week @ 2024-09-13 14/week @ 2024-09-20 15/week @ 2024-09-27 1/week @ 2024-10-04 221/week @ 2024-10-25 11/week @ 2024-11-01 316/week @ 2024-11-08 41/week @ 2024-11-15 4/week @ 2024-11-22 38/week @ 2024-12-06

90 downloads per month

MIT/Apache

26KB
299 lines

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:

Example

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.
    html_list.to_string();
}

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

The following features enable integration with popular web frameworks. See the Cargo Book to learn more about enabling features.

  • rocket: Enables integration with the Rocket web framework and allows to return HtmlFragment from handlers.
  • axum: Enables integration with the Axum web framework and allows to return HtmlFragment from handlers.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.4–32MB
~495K SLoC