axum-wire 0.0.6

Server-driven reactive UI components for Axum — like Laravel Livewire, but for Rust
Documentation
use axum_wire::html;

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn control_flow() {
    let with_else = html! {
     <div>
        {
            if true {
                "True"
            } else {
                "False"
            }
        }
     </div>
    };
    assert_eq!(with_else, "<div>True</div>");

    let with_unit = html! {
     <div>
        {()}
     </div>
    };
    assert_eq!(with_unit, "<div></div>");

    let with_none = html! {
     <div>
        {None::<&str>}
     </div>
    };
    assert_eq!(with_none, "<div></div>");

    let option_control_flow = html! {
     <div>
        {
            if true {
                Some("True")
            } else {
                None::<&str>
            }
        }
     </div>
    };
    assert_eq!(option_control_flow, "<div>True</div>");
}

/// `if` without `else` — the then-branch returns a String (from `html!`),
/// while the implicit else-branch is `()`.  Both implement `HtmlFragment`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn if_without_else_string() {
    let show = true;
    let result = html! {
        <div>
            {if show {
                html! { <p>"visible"</p> }
            }}
        </div>
    };
    assert_eq!(result, "<div><p>visible</p></div>");

    let show = false;
    let result = html! {
        <div>
            {if show {
                html! { <p>"visible"</p> }
            }}
        </div>
    };
    assert_eq!(result, "<div></div>");
}

/// `if` without `else` — then-branch returns `&str`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn if_without_else_str() {
    let show = true;
    let result = html! {
        <div>
            {if show { "hello" }}
        </div>
    };
    assert_eq!(result, "<div>hello</div>");

    let show = false;
    let result = html! {
        <div>
            {if show { "hello" }}
        </div>
    };
    assert_eq!(result, "<div></div>");
}

/// `if / else if / else` chain with different HtmlFragment types per arm.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn if_else_if_else_chain() {
    let val = 2;
    let result = html! {
        <div>
            {if val == 1 {
                html! { <span>"one"</span> }
            } else if val == 2 {
                html! { <span>"two"</span> }
            } else {
                html! { <span>"other"</span> }
            }}
        </div>
    };
    assert_eq!(result, "<div><span>two</span></div>");
}

/// `match` expression in a block.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn match_expression() {
    let val = 2;
    let result = html! {
        <div>
            {match val {
                1 => "one",
                2 => "two",
                _ => "other",
            }}
        </div>
    };
    assert_eq!(result, "<div>two</div>");
}

/// `for` loop in a block.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn for_loop() {
    let items = vec!["a", "b", "c"];
    let result = html! {
        <ul>
            {for item in &items {
                html! { <li>{item}</li> }
            }}
        </ul>
    };
    assert_eq!(result, "<ul><li>a</li><li>b</li><li>c</li></ul>");
}