pub struct HtmlPage { /* private fields */ }
Expand description

This struct represents 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

Creates a new HTML page with no content

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

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

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

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

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

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

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

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

Adds a script link with additional attributes to the HtmlPage

Adds a script link with additional attributes to the HtmlPage

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

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();

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

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();

Adds the specified style data with the specified attributes

Adds the specified style data with the specified attributes

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

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

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

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

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Convert this element into an HTML string Read more

Adds the specified HTML element to this container Read more

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

Add the container to this HTML Container Read more

Nest the specified container within this container Read more

Add the specified Table to this container Read more

Nest the specified Table within this container Read more

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

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

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

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

Adds an <img> tag to this container Read more

Adds an <img> tag to this container Read more

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

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

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

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

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

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

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

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

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

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.