pub struct HtmlTag {
pub pre_content: Option<String>,
pub tag_type: TagType,
pub class_names: Vec<String>,
pub id: Option<String>,
pub body: Option<String>,
pub children: Option<Vec<HtmlTag>>,
pub custom_attributes: Option<Vec<(String, String)>>,
}Expand description
A struct representing an HTML tag. The concept of a HTML tag is represented using this struct. It contains all the information needed to construct a HTML string.
The heart of the crate, it contains all the necessary traits and methods to construct a HTML string.
§Examples
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>");
This also has a Display implementation, so you can
print it directly.
Moreover, the elements can be nested, 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>");Hence, you can scaffold a HTML element quite easily.
§Custom Tags
You can also use custom tags, like so:
use html_tag::HtmlTag;
let mut custom = HtmlTag::new("custom");
custom.set_body("Hello World");
assert_eq!(custom.to_html(), "<custom>Hello World</custom>");Remember, all of these can be nested as well as modifies using the methods provided.
Fields§
§pre_content: Option<String>§tag_type: TagType§class_names: Vec<String>§id: Option<String>§body: Option<String>§children: Option<Vec<HtmlTag>>§custom_attributes: Option<Vec<(String, String)>>Implementations§
Source§impl HtmlTag
impl HtmlTag
Sourcepub fn new(tag_type: &str) -> HtmlTag
pub fn new(tag_type: &str) -> HtmlTag
Creates a new HtmlTag with the given tag type.
The tag type can be any valid HTML tag, or a custom tag. The crate is smart enough to handle both.
This initializes the HtmlTag with the given tag type,
although none of the other fields are initialized.
Hence, all are set to None or empty.
§Examples
use html_tag::HtmlTag;
let mut a = HtmlTag::new("a");
assert_eq!(a.to_html(), "<a></a>");Essentially a initializer for the struct.
Examples found in repository?
3fn main() {
4 let mut main = HtmlTag::new("div")
5 .with_id("main")
6 .with_style("color", "red");
7
8 let to_add = ["Ram", "Jake", "John", "Jill", "Jenny"];
9
10 to_add.iter().enumerate().for_each(|(i, name)| {
11 let mut p = HtmlTag::new("p")
12 .with_id(&format!("p-{}", i))
13 .with_class("person");
14
15 p.set_body(&format!("{}. {}", i + 1, name));
16
17 main.add_child(p);
18 });
19
20 println!("{}", main.to_html());
21}More examples
3fn main() {
4 let mut style = StyleSheet::new();
5 style.add_style(".wow", "color", "red");
6 style.add_style(".wow", "font-size", "20px");
7 style.add_style(".wow", "font-family", "sans-serif");
8
9 style.add_style("h1", "color", "blue");
10 style.add_style("h1", "font-size", "30px");
11
12 println!("{}", style.get_style_sheet());
13 println!("{}", style.get_with_tag());
14
15 // with HtmlTag
16 let div = html_tag::HtmlTag::new("div")
17 .with_id("wow")
18 .embed_style_sheet(&style)
19 .with_child(
20 html_tag::HtmlTag::new("h1")
21 .with_class("wow")
22 .with_body("Hello World"),
23 );
24
25 println!("{}", div.to_html());
26}Sourcepub fn fresh(
tag_type: TagType,
body: Option<&str>,
class_names: Vec<&str>,
) -> HtmlTag
pub fn fresh( tag_type: TagType, body: Option<&str>, class_names: Vec<&str>, ) -> HtmlTag
Creates a new HtmlTag with the given tag type and body.
This is a more pragmatic approach to creating a new HtmlTag.
You specify the exact tag type, the body, and the class names.
§Examples
use html_tag::HtmlTag;
use html_tag::TagType;
let mut a = HtmlTag::fresh(TagType::A, Some("Hello World"), vec!["test"]);
assert_eq!(a.to_html(), "<a class=\"test\">Hello World</a>");This is the most commonly used scaffold for creating a new HtmlTag.
Sourcepub fn add_child(&mut self, child: HtmlTag)
pub fn add_child(&mut self, child: HtmlTag)
Adds a child of the type HtmlTag to the current HtmlTag.
This is used to essentially nest HTML tags.
§Examples
use html_tag::HtmlTag;
let mut div = HtmlTag::new("div");
let mut p = HtmlTag::new("p");
p.set_body("Hello World");
div.add_child(p);
assert_eq!(div.to_html(), "<div><p>Hello World</p></div>");This needs a mutable reference to the current HtmlTag.
Examples found in repository?
3fn main() {
4 let mut main = HtmlTag::new("div")
5 .with_id("main")
6 .with_style("color", "red");
7
8 let to_add = ["Ram", "Jake", "John", "Jill", "Jenny"];
9
10 to_add.iter().enumerate().for_each(|(i, name)| {
11 let mut p = HtmlTag::new("p")
12 .with_id(&format!("p-{}", i))
13 .with_class("person");
14
15 p.set_body(&format!("{}. {}", i + 1, name));
16
17 main.add_child(p);
18 });
19
20 println!("{}", main.to_html());
21}Sourcepub fn set_body(&mut self, body: &str)
pub fn set_body(&mut self, body: &str)
Sets the body of the current HtmlTag.
Examples found in repository?
3fn main() {
4 let mut main = HtmlTag::new("div")
5 .with_id("main")
6 .with_style("color", "red");
7
8 let to_add = ["Ram", "Jake", "John", "Jill", "Jenny"];
9
10 to_add.iter().enumerate().for_each(|(i, name)| {
11 let mut p = HtmlTag::new("p")
12 .with_id(&format!("p-{}", i))
13 .with_class("person");
14
15 p.set_body(&format!("{}. {}", i + 1, name));
16
17 main.add_child(p);
18 });
19
20 println!("{}", main.to_html());
21}Sourcepub fn add_styles(&mut self, styles: Class)
pub fn add_styles(&mut self, styles: Class)
Construct and applies styles The Class struct is a HashMap<String, String> This is to represent the key-value pairs of the styles
§Examples
use html_tag::HtmlTag;
use html_tag::styles::Class;
let mut div = HtmlTag::new("div");
let mut font_style = Class::new();
font_style.insert("font-size".to_string(), "20px".to_string());
font_style.insert("font-family".to_string(), "sans-serif".to_string());
div.add_styles(font_style);
assert_eq!(div.to_html(), "<div style=\"font-family: sans-serif;font-size: 20px;\"></div>");Sourcepub fn with_styles(self, styles: Class) -> Self
pub fn with_styles(self, styles: Class) -> Self
Chaining method for add_styles
Sourcepub fn with_class(self, class_name: &str) -> Self
pub fn with_class(self, class_name: &str) -> Self
Chaining method for add_class
Examples found in repository?
3fn main() {
4 let mut main = HtmlTag::new("div")
5 .with_id("main")
6 .with_style("color", "red");
7
8 let to_add = ["Ram", "Jake", "John", "Jill", "Jenny"];
9
10 to_add.iter().enumerate().for_each(|(i, name)| {
11 let mut p = HtmlTag::new("p")
12 .with_id(&format!("p-{}", i))
13 .with_class("person");
14
15 p.set_body(&format!("{}. {}", i + 1, name));
16
17 main.add_child(p);
18 });
19
20 println!("{}", main.to_html());
21}More examples
3fn main() {
4 let mut style = StyleSheet::new();
5 style.add_style(".wow", "color", "red");
6 style.add_style(".wow", "font-size", "20px");
7 style.add_style(".wow", "font-family", "sans-serif");
8
9 style.add_style("h1", "color", "blue");
10 style.add_style("h1", "font-size", "30px");
11
12 println!("{}", style.get_style_sheet());
13 println!("{}", style.get_with_tag());
14
15 // with HtmlTag
16 let div = html_tag::HtmlTag::new("div")
17 .with_id("wow")
18 .embed_style_sheet(&style)
19 .with_child(
20 html_tag::HtmlTag::new("h1")
21 .with_class("wow")
22 .with_body("Hello World"),
23 );
24
25 println!("{}", div.to_html());
26}Sourcepub fn with_body(self, body: &str) -> Self
pub fn with_body(self, body: &str) -> Self
Chaining method for set_body
Examples found in repository?
3fn main() {
4 let mut style = StyleSheet::new();
5 style.add_style(".wow", "color", "red");
6 style.add_style(".wow", "font-size", "20px");
7 style.add_style(".wow", "font-family", "sans-serif");
8
9 style.add_style("h1", "color", "blue");
10 style.add_style("h1", "font-size", "30px");
11
12 println!("{}", style.get_style_sheet());
13 println!("{}", style.get_with_tag());
14
15 // with HtmlTag
16 let div = html_tag::HtmlTag::new("div")
17 .with_id("wow")
18 .embed_style_sheet(&style)
19 .with_child(
20 html_tag::HtmlTag::new("h1")
21 .with_class("wow")
22 .with_body("Hello World"),
23 );
24
25 println!("{}", div.to_html());
26}Sourcepub fn with_id(self, id: &str) -> Self
pub fn with_id(self, id: &str) -> Self
Chaining method for set_id
Examples found in repository?
3fn main() {
4 let mut main = HtmlTag::new("div")
5 .with_id("main")
6 .with_style("color", "red");
7
8 let to_add = ["Ram", "Jake", "John", "Jill", "Jenny"];
9
10 to_add.iter().enumerate().for_each(|(i, name)| {
11 let mut p = HtmlTag::new("p")
12 .with_id(&format!("p-{}", i))
13 .with_class("person");
14
15 p.set_body(&format!("{}. {}", i + 1, name));
16
17 main.add_child(p);
18 });
19
20 println!("{}", main.to_html());
21}More examples
3fn main() {
4 let mut style = StyleSheet::new();
5 style.add_style(".wow", "color", "red");
6 style.add_style(".wow", "font-size", "20px");
7 style.add_style(".wow", "font-family", "sans-serif");
8
9 style.add_style("h1", "color", "blue");
10 style.add_style("h1", "font-size", "30px");
11
12 println!("{}", style.get_style_sheet());
13 println!("{}", style.get_with_tag());
14
15 // with HtmlTag
16 let div = html_tag::HtmlTag::new("div")
17 .with_id("wow")
18 .embed_style_sheet(&style)
19 .with_child(
20 html_tag::HtmlTag::new("h1")
21 .with_class("wow")
22 .with_body("Hello World"),
23 );
24
25 println!("{}", div.to_html());
26}Sourcepub fn with_style(self, key: &str, value: &str) -> Self
pub fn with_style(self, key: &str, value: &str) -> Self
Chaining method for set_style
Examples found in repository?
3fn main() {
4 let mut main = HtmlTag::new("div")
5 .with_id("main")
6 .with_style("color", "red");
7
8 let to_add = ["Ram", "Jake", "John", "Jill", "Jenny"];
9
10 to_add.iter().enumerate().for_each(|(i, name)| {
11 let mut p = HtmlTag::new("p")
12 .with_id(&format!("p-{}", i))
13 .with_class("person");
14
15 p.set_body(&format!("{}. {}", i + 1, name));
16
17 main.add_child(p);
18 });
19
20 println!("{}", main.to_html());
21}Sourcepub fn with_child(self, child: HtmlTag) -> Self
pub fn with_child(self, child: HtmlTag) -> Self
Chaining method for add_child
Examples found in repository?
3fn main() {
4 let mut style = StyleSheet::new();
5 style.add_style(".wow", "color", "red");
6 style.add_style(".wow", "font-size", "20px");
7 style.add_style(".wow", "font-family", "sans-serif");
8
9 style.add_style("h1", "color", "blue");
10 style.add_style("h1", "font-size", "30px");
11
12 println!("{}", style.get_style_sheet());
13 println!("{}", style.get_with_tag());
14
15 // with HtmlTag
16 let div = html_tag::HtmlTag::new("div")
17 .with_id("wow")
18 .embed_style_sheet(&style)
19 .with_child(
20 html_tag::HtmlTag::new("h1")
21 .with_class("wow")
22 .with_body("Hello World"),
23 );
24
25 println!("{}", div.to_html());
26}Sourcepub fn with_attribute(self, key: &str, value: &str) -> Self
pub fn with_attribute(self, key: &str, value: &str) -> Self
Chaining method for add_attribute
Sourcepub fn set_pre_content(&mut self, body: &str)
pub fn set_pre_content(&mut self, body: &str)
Sets the pre tag of the current HtmlTag.
Sourcepub fn embed_style_sheet(self, style_sheet: &StyleSheet) -> Self
pub fn embed_style_sheet(self, style_sheet: &StyleSheet) -> Self
Embed custom StyleSheet
Examples found in repository?
3fn main() {
4 let mut style = StyleSheet::new();
5 style.add_style(".wow", "color", "red");
6 style.add_style(".wow", "font-size", "20px");
7 style.add_style(".wow", "font-family", "sans-serif");
8
9 style.add_style("h1", "color", "blue");
10 style.add_style("h1", "font-size", "30px");
11
12 println!("{}", style.get_style_sheet());
13 println!("{}", style.get_with_tag());
14
15 // with HtmlTag
16 let div = html_tag::HtmlTag::new("div")
17 .with_id("wow")
18 .embed_style_sheet(&style)
19 .with_child(
20 html_tag::HtmlTag::new("h1")
21 .with_class("wow")
22 .with_body("Hello World"),
23 );
24
25 println!("{}", div.to_html());
26}Sourcepub fn add_attribute(&mut self, key: &str, value: &str)
pub fn add_attribute(&mut self, key: &str, value: &str)
Adds an attribute to the current HtmlTag.
This attribute can be a custom attribute, or a
predefined attribute like class or id.
§Examples
use html_tag::HtmlTag;
let mut div = HtmlTag::new("div");
div.add_attribute("class", "test");
div.add_attribute("id", "test");
div.add_attribute("style", "color: red;");
assert_eq!(div.to_html(), "<div id=\"test\" class=\"test\" style=\"color: red;\"></div>");This is used to add custom attributes as well.
Sourcepub fn add_custom(&mut self, attributes: Vec<(&str, &str)>)
pub fn add_custom(&mut self, attributes: Vec<(&str, &str)>)
Adds multiple custom attributes to the current HtmlTag.
You can declare the custom attributes as a vector of tuples
of the form (&str, &str).
Where the first element of the tuple is the key, and the second element is the value.
Sourcepub fn to_html(&self) -> String
pub fn to_html(&self) -> String
Converts the current HtmlTag to a HTML string.
This is the main form of conversion, and is used
to convert the HtmlTag to a HTML string that can
be used in a HTML document.
§Examples
use html_tag::HtmlTag;
let mut div = HtmlTag::new("div");
div.add_class("test");
div.set_id("test");
assert_eq!(div.to_html(), "<div id=\"test\" class=\"test\"></div>");
This method is implemented as the display trait, so you can print it directly or use it in a format string. Like this
use html_tag::HtmlTag;
let mut div = HtmlTag::new("div");
div.add_class("test");
div.set_id("test");
println!("{}", div);This will print the following: <div class="test" id="test"></div>
Examples found in repository?
3fn main() {
4 let mut main = HtmlTag::new("div")
5 .with_id("main")
6 .with_style("color", "red");
7
8 let to_add = ["Ram", "Jake", "John", "Jill", "Jenny"];
9
10 to_add.iter().enumerate().for_each(|(i, name)| {
11 let mut p = HtmlTag::new("p")
12 .with_id(&format!("p-{}", i))
13 .with_class("person");
14
15 p.set_body(&format!("{}. {}", i + 1, name));
16
17 main.add_child(p);
18 });
19
20 println!("{}", main.to_html());
21}More examples
3fn main() {
4 let mut style = StyleSheet::new();
5 style.add_style(".wow", "color", "red");
6 style.add_style(".wow", "font-size", "20px");
7 style.add_style(".wow", "font-family", "sans-serif");
8
9 style.add_style("h1", "color", "blue");
10 style.add_style("h1", "font-size", "30px");
11
12 println!("{}", style.get_style_sheet());
13 println!("{}", style.get_with_tag());
14
15 // with HtmlTag
16 let div = html_tag::HtmlTag::new("div")
17 .with_id("wow")
18 .embed_style_sheet(&style)
19 .with_child(
20 html_tag::HtmlTag::new("h1")
21 .with_class("wow")
22 .with_body("Hello World"),
23 );
24
25 println!("{}", div.to_html());
26}