Crate htmx

source ·
Expand description

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

§htmx! macro

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

let link = "example.com";
htmx! {
    <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) {
    htmx! {
        if link {
            <a href=format!("example.com/{name}")>{name}</a>
        } else {
            {name}
        }
    }
}
htmx! {
    <Custom name="link" link/>
    " "
    <Custom name="normal"/>
}

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

link normal

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

Modules§

Macros§

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

Structs§

  • Implements From<impl IntoIterator<Item = impl Into<T>>>. This means attributes can accept more values.
  • Canonical type to accept children in component.
  • Allows creating an element with arbitary tag name and attributes.
  • Html
  • Html boilderplate
  • Puts content directly into HTML bypassing HTML-escaping.

Traits§

  • Converts to Html, either by appending to existing Html or by creating a new one.
  • Trait used with the custom Rust like JS in <script> tags using the htmx! macro.

Attribute Macros§

  • Allows to make a component from a function or struct.