Struct build_html::HtmlPage
source · pub struct HtmlPage { /* private fields */ }
Expand description
An entire page of HTML which can built up by chaining addition methods.
To convert an HtmlPage
to a String
which can be sent back to a client, use the
Html::to_html_string()
method
Example
let page: String = HtmlPage::new()
.with_title("My Page")
.with_header(1, "Header Text")
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head><title>My Page</title></head>",
"<body><h1>Header Text</h1></body></html>"
));
Implementations§
source§impl HtmlPage
impl HtmlPage
sourcepub fn with_version(version: HtmlVersion) -> Self
pub fn with_version(version: HtmlVersion) -> Self
Create a new HTML page with the specified version.
Example
assert_eq!(
HtmlPage::with_version(HtmlVersion::HTML4).to_html_string(),
concat!(
r#"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "#,
r#""http://www.w3.org/TR/HTML4/loose.dtd">"#,
"<html><head></head><body></body></html>",
),
)
sourcepub fn add_head_link(&mut self, href: impl ToString, rel: impl ToString)
pub fn add_head_link(&mut self, href: impl ToString, rel: impl ToString)
Adds a new link element to the HTML head.
Example
let mut page = HtmlPage::new();
page.add_head_link("favicon.ico", "icon");
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head>",
r#"<link href="favicon.ico" rel="icon">"#,
"</head><body></body></html>"
));
sourcepub fn with_head_link(self, href: impl ToString, rel: impl ToString) -> Self
pub fn with_head_link(self, href: impl ToString, rel: impl ToString) -> Self
Adds a new link to the HTML head.
Example
let page = HtmlPage::new()
.with_head_link("favicon.ico", "icon")
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head>",
r#"<link href="favicon.ico" rel="icon">"#,
"</head><body></body></html>"
));
sourcepub fn add_head_link_attr<A, S>(
&mut self,
href: impl ToString,
rel: impl ToString,
attr: A
)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn add_head_link_attr<A, S>( &mut self, href: impl ToString, rel: impl ToString, attr: A )where A: IntoIterator<Item = (S, S)>, S: ToString,
Adds a new link to the HTML head with the specified additional attributes
Example
let mut page = HtmlPage::new();
page.add_head_link_attr("print.css", "stylesheet", [("media", "print")]);
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head>",
r#"<link href="print.css" rel="stylesheet" media="print">"#,
"</head><body></body></html>"
));
sourcepub fn with_head_link_attr<A, S>(
self,
href: impl ToString,
rel: impl ToString,
attr: A
) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn with_head_link_attr<A, S>( self, href: impl ToString, rel: impl ToString, attr: A ) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
Adds a new link to the HTML head with the specified additional attributes
Example
let page = HtmlPage::new()
.with_head_link_attr("print.css", "stylesheet", [("media", "print")])
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head>",
r#"<link href="print.css" rel="stylesheet" media="print">"#,
"</head><body></body></html>"
));
sourcepub fn add_meta<A, S>(&mut self, attributes: A)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn add_meta<A, S>(&mut self, attributes: A)where A: IntoIterator<Item = (S, S)>, S: ToString,
Adds the specified metadata elements to this HtmlPage
Attributes are specified in a HashMap
Example
let mut page = HtmlPage::new();
page.add_meta(vec![("charset", "utf-8")]);
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head>",
r#"<meta charset="utf-8">"#,
"</head><body></body></html>"
));
sourcepub fn with_meta<A, S>(self, attributes: A) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn with_meta<A, S>(self, attributes: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
Adds the specified metadata elements to this HtmlPage
Attributes are specified in a HashMap
Example
let page = HtmlPage::new()
.with_meta(vec![("charset", "utf-8")])
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head>",
r#"<meta charset="utf-8">"#,
"</head><body></body></html>"
));
sourcepub fn add_script_link(&mut self, src: impl ToString)
pub fn add_script_link(&mut self, src: impl ToString)
Adds the specified external script to the HtmlPage
Example
let mut page = HtmlPage::new();
page.add_script_link("myScript.js");
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head>",
r#"<script src="myScript.js"></script>"#,
"</head><body></body></html>"
));
sourcepub fn with_script_link(self, src: impl ToString) -> Self
pub fn with_script_link(self, src: impl ToString) -> Self
Adds the specified external script to the HtmlPage
Example
let page = HtmlPage::new()
.with_script_link("myScript.js")
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head>",
r#"<script src="myScript.js"></script>"#,
"</head><body></body></html>"
));
sourcepub fn add_script_link_attr<A, S>(&mut self, src: impl ToString, attributes: A)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn add_script_link_attr<A, S>(&mut self, src: impl ToString, attributes: A)where A: IntoIterator<Item = (S, S)>, S: ToString,
Adds a script link with additional attributes to the HtmlPage
sourcepub fn with_script_link_attr<A, S>(
self,
src: impl ToString,
attributes: A
) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn with_script_link_attr<A, S>( self, src: impl ToString, attributes: A ) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
Adds a script link with additional attributes to the HtmlPage
sourcepub fn add_script_literal(&mut self, code: impl ToString)
pub fn add_script_literal(&mut self, code: impl ToString)
Adds the specified script to this HtmlPage
Example
let mut page = HtmlPage::new();
page.add_script_literal(r#"window.onload = () => console.log("Hello World");"#);
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head><script>",
r#"window.onload = () => console.log("Hello World");"#,
"</script></head><body></body></html>"
));
In order to lint the code, it can be helpful to define your script in
its own file. That file can be inserted into the html page using the
include_str
macro:
let mut page = HtmlPage::new();
page.add_script_literal(include_str!("myScript.js"));
sourcepub fn with_script_literal(self, code: impl ToString) -> Self
pub fn with_script_literal(self, code: impl ToString) -> Self
Adds the specified script to this HtmlPage
Example
let page = HtmlPage::new()
.with_script_literal(r#"window.onload = () => console.log("Hello World");"#)
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head><script>",
r#"window.onload = () => console.log("Hello World");"#,
"</script></head><body></body></html>"
));
In order to lint the code, it can be helpful to define your script in
its own file. That file can be inserted into the html page using the
include_str
macro:
let page = HtmlPage::new()
.with_script_literal(include_str!("myScript.js"))
.to_html_string();
sourcepub fn add_style(&mut self, css: impl ToString)
pub fn add_style(&mut self, css: impl ToString)
Adds raw style data to this HtmlPage
Example
let mut page = HtmlPage::new();
page.add_style(r#"p{font-family:"Liberation Serif";}"#);
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head>",
r#"<style>p{font-family:"Liberation Serif";}</style>"#,
"</head><body></body></html>"
));
To allow for linting, it can be helpful to define CSS in its own file.
That file can be included at compile time using the include_str
macro:
let mut page = HtmlPage::new();
page.add_style(include_str!("styles.css"));
sourcepub fn with_style(self, css: impl ToString) -> Self
pub fn with_style(self, css: impl ToString) -> Self
Adds raw style data to this HtmlPage
Example
let page = HtmlPage::new()
.with_style(r#"p{font-family:"Liberation Serif";}"#)
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head>",
r#"<style>p{font-family:"Liberation Serif";}</style>"#,
"</head><body></body></html>"
));
To allow for linting, it can be helpful to define CSS in its own file.
That file can be included at compile time using the include_str
macro:
let page = HtmlPage::new()
.with_style(include_str!("styles.css"))
.to_html_string();
sourcepub fn add_style_attr<A, S>(&mut self, css: impl ToString, attributes: A)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn add_style_attr<A, S>(&mut self, css: impl ToString, attributes: A)where A: IntoIterator<Item = (S, S)>, S: ToString,
Adds the specified style data with the specified attributes
sourcepub fn with_style_attr<A, S>(self, css: impl ToString, attributes: A) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn with_style_attr<A, S>(self, css: impl ToString, attributes: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
Adds the specified style data with the specified attributes
sourcepub fn add_stylesheet(&mut self, source: impl ToString)
pub fn add_stylesheet(&mut self, source: impl ToString)
Adds the specified stylesheet to the HTML head.
This method uses add_head_link
internally
Example
let mut page = HtmlPage::new();
page.add_stylesheet("print.css");
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head>",
r#"<link href="print.css" rel="stylesheet">"#,
"</head><body></body></html>"
));
sourcepub fn with_stylesheet(self, source: impl ToString) -> Self
pub fn with_stylesheet(self, source: impl ToString) -> Self
Adds the specified stylesheet to the HTML head.
This method uses add_head_link
internally
Example
let page = HtmlPage::new()
.with_stylesheet("print.css")
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head>",
r#"<link href="print.css" rel="stylesheet">"#,
"</head><body></body></html>"
));
sourcepub fn add_title(&mut self, title_text: impl ToString)
pub fn add_title(&mut self, title_text: impl ToString)
Adds a title to this HTML page
Example
let mut page = HtmlPage::new();
page.add_title("My Page");
assert_eq!(page.to_html_string(), concat!(
"<!DOCTYPE html><html><head>",
"<title>My Page</title>",
"</head><body></body></html>"
));
sourcepub fn with_title(self, title_text: impl ToString) -> Self
pub fn with_title(self, title_text: impl ToString) -> Self
Adds a title to this HTML page
Example
let page = HtmlPage::new()
.with_title("My Page")
.to_html_string();
assert_eq!(page, concat!(
"<!DOCTYPE html><html><head>",
"<title>My Page</title>",
"</head><body></body></html>"
));
Trait Implementations§
source§impl Html for HtmlPage
impl Html for HtmlPage
source§fn to_html_string(&self) -> String
fn to_html_string(&self) -> String
source§impl HtmlContainer for HtmlPage
impl HtmlContainer for HtmlPage
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)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn add_header_attr<A, S>(&mut self, level: u8, text: impl ToString, attr: A)where A: IntoIterator<Item = (S, S)>, S: ToString,
source§fn with_header_attr<A, S>(self, level: u8, text: impl ToString, attr: A) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn with_header_attr<A, S>(self, level: u8, text: impl ToString, attr: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
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
)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn add_image_attr<A, S>( &mut self, src: impl ToString, alt: impl ToString, attr: A )where A: IntoIterator<Item = (S, S)>, S: ToString,
<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
) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn with_image_attr<A, S>( self, src: impl ToString, alt: impl ToString, attr: A ) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
<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
)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn add_link_attr<A, S>( &mut self, href: impl ToString, text: impl ToString, attr: A )where A: IntoIterator<Item = (S, S)>, S: ToString,
<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
) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn with_link_attr<A, S>( self, href: impl ToString, text: impl ToString, attr: A ) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
<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)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn add_paragraph_attr<A, S>(&mut self, text: impl ToString, attr: A)where A: IntoIterator<Item = (S, S)>, S: ToString,
<p>
tag element with the specified attributes to this Container Read moresource§fn with_paragraph_attr<A, S>(self, text: impl ToString, attr: A) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn with_paragraph_attr<A, S>(self, text: impl ToString, attr: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
<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)where
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn add_preformatted_attr<A, S>(&mut self, text: impl ToString, attr: A)where A: IntoIterator<Item = (S, S)>, S: ToString,
<pre>
tag element with the specified attributes to this container Read moresource§fn with_preformatted_attr<A, S>(self, text: impl ToString, attr: A) -> Selfwhere
A: IntoIterator<Item = (S, S)>,
S: ToString,
fn with_preformatted_attr<A, S>(self, text: impl ToString, attr: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,
<pre>
tag element with the specified attributes to this container Read more