Crate htmx

source ·
Expand description

Library for doing server side rendering of HTML using a macro.

§html! macro

The html! macro allows to write HTML inside your rust code allowing to include rust values and instantiate custom components.

let link = "example.com";
html! {
    <div>
        "Some literal text "
        // In attributes, expressions can be used directly.
        <a href=link>
            // In bodies braces are required.
            {link}
        </a>
        if 1 < 2 {
            <p>
                // Whitespace must be inside `"strings"`.
                <code> "if" </code> ", " <code> "for" </code>
                ", and " <code> "while" </code> " can be used as well."
            </p>
        }
    </_> // Closing tags can be inferred.
}

Will result in (with some added whitespace for readability).

<!DOCTYPE html>
<div>
    Some literal text <a href="example.com">example.com</a>
    <p> <code>if</code>, <code>for</code>, and
    <code>while</code> can be used as well. </p>
</div>
Some literal text example.com

if, for, and while can be used as well.

§Custom Components

The most powerful feature of this crate are custom components. Using the component macro, they can be created, based on structs or functions. Similarly to react or leptos.


#[component]
fn Custom(name: String, link: bool) {
    html! {
        if link {
            <a href=format!("example.com/{name}")>{name}</a>
        } else {
            {name}
        }
    }
}
html! {
    <Custom name="link" link/>
    " "
    <Custom name="normal"/>
}

Will result in <a href="example.com/link">link</a> normal:

link normal

For more documentation see html!, component and the examples.

Modules§

Macros§

  • The html! macro allows constructing Html using an HTML like syntax.

Structs§

Traits§

Attribute Macros§

  • Allows to make a component from a function.

Derive Macros§