HtmlTag

Struct HtmlTag 

Source
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

Source

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?
examples/basic.rs (line 4)
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
Hide additional examples
examples/styles.rs (line 16)
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}
Source

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.

Source

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?
examples/basic.rs (line 17)
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}
Source

pub fn add_class(&mut self, class_name: &str)

Adds a class name to the current HtmlTag.

Source

pub fn set_body(&mut self, body: &str)

Sets the body of the current HtmlTag.

Examples found in repository?
examples/basic.rs (line 15)
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}
Source

pub fn set_id(&mut self, id: &str)

Sets the id of the current HtmlTag.

Source

pub fn set_href(&mut self, href: &str)

Sets the href of the current HtmlTag.

Source

pub fn set_style(&mut self, key: &str, value: &str)

Sets the style of the current HtmlTag.

Source

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>");
Source

pub fn with_styles(self, styles: Class) -> Self

Chaining method for add_styles

Source

pub fn with_class(self, class_name: &str) -> Self

Chaining method for add_class

Examples found in repository?
examples/basic.rs (line 13)
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
Hide additional examples
examples/styles.rs (line 21)
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}
Source

pub fn with_body(self, body: &str) -> Self

Chaining method for set_body

Examples found in repository?
examples/styles.rs (line 22)
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}
Source

pub fn with_id(self, id: &str) -> Self

Chaining method for set_id

Examples found in repository?
examples/basic.rs (line 5)
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
Hide additional examples
examples/styles.rs (line 17)
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}
Source

pub fn with_href(self, href: &str) -> Self

Chaining method for set_href

Source

pub fn with_style(self, key: &str, value: &str) -> Self

Chaining method for set_style

Examples found in repository?
examples/basic.rs (line 6)
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}
Source

pub fn with_child(self, child: HtmlTag) -> Self

Chaining method for add_child

Examples found in repository?
examples/styles.rs (lines 19-23)
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}
Source

pub fn with_attribute(self, key: &str, value: &str) -> Self

Chaining method for add_attribute

Source

pub fn set_pre_content(&mut self, body: &str)

Sets the pre tag of the current HtmlTag.

Source

pub fn embed_style_sheet(self, style_sheet: &StyleSheet) -> Self

Embed custom StyleSheet

Examples found in repository?
examples/styles.rs (line 18)
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}
Source

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.

Source

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.

Source

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?
examples/basic.rs (line 20)
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
Hide additional examples
examples/styles.rs (line 25)
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}
Source

pub fn construct(&self) -> String

Just a fancy name for to_html.

Trait Implementations§

Source§

impl Clone for HtmlTag

Source§

fn clone(&self) -> HtmlTag

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Display for HtmlTag

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for HtmlTag

Source§

fn eq(&self, other: &HtmlTag) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for HtmlTag

Source§

impl StructuralPartialEq for HtmlTag

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.