Crates.io | html_tag |
lib.rs | html_tag |
version | 0.1.3 |
source | src |
created_at | 2023-12-28 13:06:19.64599 |
updated_at | 2024-01-28 15:37:46.651834 |
description | An Enigmatic Way to use HTML in Rust |
homepage | |
repository | https://github.com/newtoallofthis123/html_tag |
max_upload_size | |
id | 1082404 |
size | 35,500 |
HTML Tag, the enigmatic way to write HTML in your Rust code.
HTML Tag is a wrapper struct that allows you to write HTML in your Rust code.
It essentially removes the use of raw strings or the need to use macros such as the html!
macro from the html
crate to return a String
of HTML.
When I was experimenting with the HTMX library, I found that I was writing a lot of raw HTML strings in my Rust code.
Stuff like this
find_files(&search.q).iter().for_each(|x| {
html += format!("<p><a class='link' href='/assets/{}'>{}</a></p>", x, x).as_str();
});
was not very readable and was prone to errors. Hence I wanted to find a way to write HTML in my Rust code in a more readable and debug-able way.
Enter HTML Tag.
Using HTML Tag, the above code can be written as
html = HtmlTag::new("div");
find_files(&search.q).iter().for_each(|x| {
let mut tag = HtmlTag::new("p")
.add_child(HtmlTag::new("a")
.add_class("link")
.set_href(format!("/assets/{}", x))
.set_body(x));
html += tag.to_string();
});
HTML Tag is very simple to use.
Basically, you create a new HtmlTag
struct with the tag name as the argument.
Then you can add attributes to the tag using the add_attribute
method.
However, for the common attributes and classes, there are methods that you can use to add them.
You can go through the documentation for more information.
First up, html_tag is not a replacement for the html!
macro from the html
crate.
It is more of a wrapper struct that allows you to write HTML in your Rust code.
You can use it when you want to write HTML in your Rust code, especially when you are dealing with HATEOAS APIs.
If you want full HTML generation you are better off using something like html_builder
Moreover, this also means that you can have modular control of the html string that you are generating.
Hence, you can do something like this for example
let mut html = HtmlTag::new("div");
let interested = true
if interested {
html.with_child(HtmlTag::new("p").with_body("I am interested"));
} else {
html.with_child(HtmlTag::new("p").with_body("I am not interested"));
}
This is a very simple example, but you can see how you can have modular control over the HTML string that you are generating.
If you have any ideas on how to improve this crate, feel free to open an issue or a pull request.
I am a complete noob when it comes to Rust and especially when it comes to writing crates that are well optimised and well documented. So any help is appreciated.
Some areas that I would like to improve on are
Any of the above or any other improvements are welcome.
This crate is licensed under the MIT License. You can read the license here.