templating 0.1.2

Simple HTML templating for Rust.
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented2 out of 2 items with examples
  • Size
  • Source code size: 14.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 271.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Surjuu/templating
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Surjuu

crates.io docs.rs

Templating

This crate provides the html macro for creating HTML templates in a simple way. More information about the macro can be found in its documentation.

Tutorial

Simple templates

You can use the html macro to create a tamplate like this:

fn home() -> String {
    html! {
        <span>
            Welcome to my page
        </span>
    }
}

Expression templates

You can use parentheses to insert a runtime value into the string.

fn dashboard(user: &str) -> String {
    html! {
        <h3>
            Hello, (user)
        </h3>
    }
}

But if you try this, you will get an unexpected result:

dashboard("Rust") // => <h3>Hello,Rust</h3>

If you want to add an space, you can use templates:

fn dashboard(user: &str) -> String {
    html! {
        <h3>
            ("Hello, ")(user)
        </h3>
    }
}
dashboard("Rust") // => <h3>Hello, Rust</h3>

Block templates

You can use block templates to create more complex templates:

fn store(items: Vec<String>) -> String {
    html! {
        <ul>
            {
                for item in items {
                    html! {
                        <li>
                            (item)
                        </li>
                    };
                }
            }
        </ul>
    }
}