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
impl HtmlElement
Sourcepub fn new(tag: HtmlTag) -> Self
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/>");
Sourcepub fn add_child(&mut self, content: HtmlChild)
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 &str
s and
HtmlElement
s 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>");
Sourcepub fn with_child(self, content: HtmlChild) -> Self
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 &str
s and
HtmlElement
s 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>");
Sourcepub fn add_attribute(&mut self, k: impl ToString, v: impl ToString)
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"/>"#);
Sourcepub fn with_attribute(self, k: impl ToString, v: impl ToString) -> Self
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
impl Clone for HtmlElement
Source§fn clone(&self) -> HtmlElement
fn clone(&self) -> HtmlElement
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for HtmlElement
impl Debug for HtmlElement
Source§impl Display for HtmlElement
impl Display for HtmlElement
Source§impl From<HtmlElement> for HtmlChild
impl From<HtmlElement> for HtmlChild
Source§fn from(value: HtmlElement) -> Self
fn from(value: HtmlElement) -> Self
Source§impl Html for HtmlElement
impl Html for HtmlElement
Source§fn to_html_string(&self) -> String
fn to_html_string(&self) -> String
Source§impl HtmlContainer for HtmlElement
This implementation of HtmlContainer allows seamless for compatibility between the “easy”
interface and this more complete one
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)
fn add_html<H: Html>(&mut self, html: H)
Source§fn with_html<H: Html>(self, html: H) -> Self
fn with_html<H: Html>(self, html: H) -> Self
Source§fn add_container(&mut self, container: Container)
fn add_container(&mut self, container: Container)
Source§fn with_container(self, container: Container) -> Self
fn with_container(self, container: Container) -> Self
Source§fn with_table(self, table: Table) -> Self
fn with_table(self, table: Table) -> Self
Table
within this container Read moreSource§fn add_header(&mut self, level: u8, text: impl ToString)
fn add_header(&mut self, level: u8, text: impl ToString)
Source§fn with_header(self, level: u8, text: impl ToString) -> Self
fn with_header(self, level: u8, text: impl ToString) -> Self
Source§fn add_header_attr<A, S>(&mut self, level: u8, text: impl ToString, attr: A)
fn add_header_attr<A, S>(&mut self, level: u8, text: impl ToString, attr: A)
Source§fn with_header_attr<A, S>(self, level: u8, text: impl ToString, attr: A) -> Self
fn with_header_attr<A, S>(self, level: u8, text: impl ToString, attr: A) -> Self
Source§fn add_image(&mut self, src: impl ToString, alt: impl ToString)
fn add_image(&mut self, src: impl ToString, alt: impl ToString)
<img>
tag to this container Read moreSource§fn with_image(self, src: impl ToString, alt: impl ToString) -> Self
fn with_image(self, src: impl ToString, alt: impl ToString) -> Self
<img>
tag to this container Read moreSource§fn add_image_attr<A, S>(
&mut self,
src: impl ToString,
alt: impl ToString,
attr: A,
)
fn add_image_attr<A, S>( &mut self, src: impl ToString, alt: impl ToString, attr: A, )
<img>
tag with the specified attributes to this container Read moreSource§fn with_image_attr<A, S>(
self,
src: impl ToString,
alt: impl ToString,
attr: A,
) -> Self
fn with_image_attr<A, S>( self, src: impl ToString, alt: impl ToString, attr: A, ) -> Self
<img>
tag with the specified attributes to this container Read moreSource§fn add_link(&mut self, href: impl ToString, text: impl ToString)
fn add_link(&mut self, href: impl ToString, text: impl ToString)
<a>
tag to this container Read moreSource§fn with_link(self, href: impl ToString, text: impl ToString) -> Self
fn with_link(self, href: impl ToString, text: impl ToString) -> Self
<a>
tag to this container Read moreSource§fn add_link_attr<A, S>(
&mut self,
href: impl ToString,
text: impl ToString,
attr: A,
)
fn add_link_attr<A, S>( &mut self, href: impl ToString, text: impl ToString, attr: A, )
<a>
tag with the specified attributes to this container Read moreSource§fn with_link_attr<A, S>(
self,
href: impl ToString,
text: impl ToString,
attr: A,
) -> Self
fn with_link_attr<A, S>( self, href: impl ToString, text: impl ToString, attr: A, ) -> Self
<a>
tag with the specified attributes to this container Read moreSource§fn add_paragraph(&mut self, text: impl ToString)
fn add_paragraph(&mut self, text: impl ToString)
<p>
tag element to this Container Read moreSource§fn with_paragraph(self, text: impl ToString) -> Self
fn with_paragraph(self, text: impl ToString) -> Self
<p>
tag element to this Container Read moreSource§fn add_paragraph_attr<A, S>(&mut self, text: impl ToString, attr: A)
fn add_paragraph_attr<A, S>(&mut self, text: impl ToString, attr: A)
<p>
tag element with the specified attributes to this Container Read moreSource§fn with_paragraph_attr<A, S>(self, text: impl ToString, attr: A) -> Self
fn with_paragraph_attr<A, S>(self, text: impl ToString, attr: A) -> Self
<p>
tag element with the specified attributes to this Container Read moreSource§fn add_preformatted(&mut self, text: impl ToString)
fn add_preformatted(&mut self, text: impl ToString)
<pre>
tag element to this container Read moreSource§fn with_preformatted(self, text: impl ToString) -> Self
fn with_preformatted(self, text: impl ToString) -> Self
<pre>
tag element to this container Read moreSource§fn add_preformatted_attr<A, S>(&mut self, text: impl ToString, attr: A)
fn add_preformatted_attr<A, S>(&mut self, text: impl ToString, attr: A)
<pre>
tag element with the specified attributes to this container Read moreSource§fn with_preformatted_attr<A, S>(self, text: impl ToString, attr: A) -> Self
fn with_preformatted_attr<A, S>(self, text: impl ToString, attr: A) -> Self
<pre>
tag element with the specified attributes to this container Read more