Crates.io | rempl-macros |
lib.rs | rempl-macros |
version | 0.3.1 |
source | src |
created_at | 2024-08-07 12:59:03.287055 |
updated_at | 2024-08-08 13:59:13.758848 |
description | A simple library for creating html components directly in your source |
homepage | https://git.sr.ht/~yuiyukihira/rempl |
repository | https://git.sr.ht/~yuiyukihira/rempl |
max_upload_size | |
id | 1328403 |
size | 55,954 |
Rempl is a simple library that adds two macros to allow you to make functions that return html easily by embedding the html directly in your source code. No templates required!
To get started with rempl simply run
$ cargo add rempl
in your terminal.
Rempl adds two simple macros. First, the html!
macro which allows you to create html sections directly within your rust source. Example:
let classes = vec!["bg-red", "b-solid"];
let msg = "Hello, world";
html! {
<p class={ classes.join(" ") }>{ msg }</p>
}
As you can see, you can pass any rust expression as an HTML element attribute, or if it implments the Display
trait, directly in the html itself.
The second macro is the component
macro, which allows you to create custom html tags, that you can call within your html!
invocations. Example:
fn Echo(class: String, children: HtmlNode) -> HtmlTerm {
html! {
<p class={ class }>{ children }</p>
}
}
fn main() {
let r = html! {
<@Echo class="Test">
"Hello, world!"
</Echo>
}
}
Components can be called by using putting a @
in front of the tag name, any attributes will be passed as parameters to the function, except for the inner html of the tag, which will be passed in the required children
parameter.
You can render your html output to a String
by simpling calling .to_string()
on the HtmlTerm
.
If you're running a nightly compiler, you can enable the nightly
feature to enable
some better diagnositics when writing invalid HTML.
This project is licensed under the Mozilla Public License which you can find here (LICENSE) or https://www.mozilla.org/en-US/MPL/2.0/.