HtmlElement

Struct HtmlElement 

Source
pub struct HtmlElement {
    pub tag: HtmlTag,
    pub attributes: Vec<(String, String)>,
    pub children: Vec<HtmlChild>,
}
Expand description

Basic Building Block: A structured HTML element, with a tag, attributes, and children.

This allows much greater flexibility than the traditional HtmlContainer interface. However, some users may still find the HtmlContainer interface more convienent than manually creating each individual element. You can use the HtmlContainer::add_raw and HtmlChild::Raw escape hatches to combine the two approaches by treating the “chunks” as strings.

let output = HtmlElement::new(HtmlTag::Div)
    .with_child(
        HtmlElement::new(HtmlTag::Heading1)
            .with_attribute("class", "big-text")
            .with_child("Header Text".into())
            .into(),
    )
    .with_child(
        HtmlElement::new(HtmlTag::ParagraphText)
            .with_child("Paragraph Text".into())
            .with_child(HtmlElement::new(HtmlTag::LineBreak).into())
            .with_child("Paragraph Text Line 2".into())
            .into(),
    )
    .to_html_string();

assert_eq!(output, r#"<div><h1 class="big-text">Header Text</h1><p>Paragraph Text<br/>Paragraph Text Line 2</p></div>"#);

Fields§

§tag: HtmlTag

The tag to be used for this element

§attributes: Vec<(String, String)>

A list of the attributes that will be printed in this element in the form (key, value)

§children: Vec<HtmlChild>

A list of the child elements contained within this element

Implementations§

Source§

impl HtmlElement

Source

pub fn new(tag: HtmlTag) -> Self

Create a new empty HTML element with the given tag

assert_eq!(HtmlElement::new(HtmlTag::Div).to_html_string(), "<div/>");
Source

pub fn add_child(&mut self, content: HtmlChild)

Add a new child to this element

A child can be either a raw string (HtmlChild::Raw) or another element (HtmlChild::Element). You can use the into function to append &strs and HtmlElements directly.

let mut element = HtmlElement::new(HtmlTag::ParagraphText);
element.add_child("First Line".into());
element.add_child(HtmlElement::new(HtmlTag::LineBreak).into());
element.add_child("Second Line".into());
assert_eq!(element.to_html_string(), "<p>First Line<br/>Second Line</p>");
Source

pub fn with_child(self, content: HtmlChild) -> Self

Consume this element and return it with the new child appended

A child can be either a raw string (HtmlChild::Raw) or another element (HtmlChild::Element). You can use the into function to append &strs and HtmlElements directly.

let output = HtmlElement::new(HtmlTag::ParagraphText)
    .with_child("First Line".into())
    .with_child(HtmlElement::new(HtmlTag::LineBreak).into())
    .with_child("Second Line".into())
    .to_html_string();
assert_eq!(output, "<p>First Line<br/>Second Line</p>");
Source

pub fn add_attribute(&mut self, k: impl ToString, v: impl ToString)

Add an attribute to this element

This attribute will simply be appended to the others that have been specified. If the same attribute is specified twice, it will be duplicated, which may result in strange behavior.

let mut element = HtmlElement::new(HtmlTag::Div);
element.add_attribute("class", "container");
assert_eq!(element.to_html_string(), r#"<div class="container"/>"#);
Source

pub fn with_attribute(self, k: impl ToString, v: impl ToString) -> Self

Consume this element and return it with the given attribute set.

This attribute will simply be appended to the others that have been specified. If the same attribute is specified twice, it will be duplicated, which may result in strange behavior.

let output = HtmlElement::new(HtmlTag::Div)
    .with_attribute("class", "container")
    .with_attribute("id", "first-div")
    .to_html_string();
assert_eq!(output, r#"<div class="container" id="first-div"/>"#);

Trait Implementations§

Source§

impl Clone for HtmlElement

Source§

fn clone(&self) -> HtmlElement

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 Debug for HtmlElement

Source§

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

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

impl Display for HtmlElement

Source§

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

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

impl From<HtmlElement> for HtmlChild

Source§

fn from(value: HtmlElement) -> Self

Converts to this type from the input type.
Source§

impl Html for HtmlElement

Source§

fn to_html_string(&self) -> String

Convert this element into an HTML string Read more
Source§

impl HtmlContainer for HtmlElement

This implementation of HtmlContainer allows seamless for compatibility between the “easy” interface and this more complete one

Source§

fn add_html<H: Html>(&mut self, html: H)

Adds the specified HTML element to this container Read more
Source§

fn with_html<H: Html>(self, html: H) -> Self

Consumes the container, returning it with the specified HTML element added to it Read more
Source§

fn add_container(&mut self, container: Container)

Add the container to this HTML Container Read more
Source§

fn with_container(self, container: Container) -> Self

Nest the specified container within this container Read more
Source§

fn add_table(&mut self, table: Table)

Add the specified Table to this container Read more
Source§

fn with_table(self, table: Table) -> Self

Nest the specified Table within this container Read more
Source§

fn add_header(&mut self, level: u8, text: impl ToString)

Adds a header tag with the designated level to this container Read more
Source§

fn with_header(self, level: u8, text: impl ToString) -> Self

Adds a header tag with the designated level to this container Read more
Source§

fn add_header_attr<A, S>(&mut self, level: u8, text: impl ToString, attr: A)
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds a header tag with the designated level and attributes to this container. Read more
Source§

fn with_header_attr<A, S>(self, level: u8, text: impl ToString, attr: A) -> Self
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds a header tag with the designated level and attributes to this container. Read more
Source§

fn add_image(&mut self, src: impl ToString, alt: impl ToString)

Adds an <img> tag to this container Read more
Source§

fn with_image(self, src: impl ToString, alt: impl ToString) -> Self

Adds an <img> tag to this container Read more
Source§

fn add_image_attr<A, S>( &mut self, src: impl ToString, alt: impl ToString, attr: A, )
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds an <img> tag with the specified attributes to this container Read more
Source§

fn with_image_attr<A, S>( self, src: impl ToString, alt: impl ToString, attr: A, ) -> Self
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds an <img> tag with the specified attributes to this container Read more
Adds an <a> tag to this container Read more
Adds an <a> tag to this container Read more
Adds an <a> tag with the specified attributes to this container Read more
Adds an <a> tag with the specified attributes to this container Read more
Source§

fn add_paragraph(&mut self, text: impl ToString)

Adds a <p> tag element to this Container Read more
Source§

fn with_paragraph(self, text: impl ToString) -> Self

Adds a <p> tag element to this Container Read more
Source§

fn add_paragraph_attr<A, S>(&mut self, text: impl ToString, attr: A)
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds a <p> tag element with the specified attributes to this Container Read more
Source§

fn with_paragraph_attr<A, S>(self, text: impl ToString, attr: A) -> Self
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds a <p> tag element with the specified attributes to this Container Read more
Source§

fn add_preformatted(&mut self, text: impl ToString)

Adds a <pre> tag element to this container Read more
Source§

fn with_preformatted(self, text: impl ToString) -> Self

Adds a <pre> tag element to this container Read more
Source§

fn add_preformatted_attr<A, S>(&mut self, text: impl ToString, attr: A)
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds a <pre> tag element with the specified attributes to this container Read more
Source§

fn with_preformatted_attr<A, S>(self, text: impl ToString, attr: A) -> Self
where A: IntoIterator<Item = (S, S)>, S: ToString,

Adds a <pre> tag element with the specified attributes to this container Read more
Source§

fn add_raw(&mut self, content: impl ToString)

Add raw content to the container. This content is pasted directly into the HTML Read more
Source§

fn with_raw(self, content: impl ToString) -> Self

Add raw content to this container. The content is pasted directly into the HTML Read more

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.