Crate html_tag

source ·
Expand description

§HTMLTag

The enigmatic way to use HTML in Rust.

HTMLTag is a crate that allows you to create HTML tags in a simple and intuitive way. It aims to replace the need for using HTML in the form of strings or the heavy use of macros in your code to make it more readable and maintainable.

§Idea

Strings are not always the best way to represent HTML in your code. You could probably use macros to make it more readable, but that is not always the best solution.

Moreover, you could probably use a templating engine or a library like html_builder to generate large HTML documents, however, we are talking niche use cases here.

The idea is to make it easy to create HTML tags in as simple and as “Rusty” way as possible.

§Usage

use html_tag::HtmlTag;

let mut a = HtmlTag::new("a");
a.set_body("Hello World");
a.add_class("test");
a.set_href("https://example.com");

assert_eq!(a.to_html(), "<a class=\"test\" href=\"https://example.com\">Hello World</a>");

As apparent from the example above, you can essentially let HtmlTag act as a builder or rather a wrapper around the abstract concept of a HTML tag.

You can also nest tags, like so:

use html_tag::HtmlTag;

let mut div = HtmlTag::new("div");
div.add_class("test");
let mut p = HtmlTag::new("p");
p.set_body("Hello World");
div.add_child(p);

assert_eq!(div.to_html(), "<div class=\"test\"><p>Hello World</p></div>");

§Main Features

  • Create HTML tags in a simple and intuitive way.
  • Chain methods to make it more readable.
  • Nest tags inside each other.
  • Use custom tags.
  • Use custom attributes.
  • Use Display trait to print the HTML tag.
  • No dependencies.

§Contributing

Contributions are welcome. Please open an issue or a PR if you find any bugs or have any suggestions.

§License

This project is licensed under the MIT License.

Re-exports§

Modules§

  • HTMLTag Related Stuff
  • StyleSheet Related Stuff
  • TagType Related Stuff