Skip to main content

Template

Struct Template 

Source
pub struct Template { /* private fields */ }
Expand description

Template for composing multiple semantic elements into a page structure.

Provides a high-level API for building accessible HTML5 document layouts.

§Examples

use html_generator::elements::Template;

let page = Template::new()
    .nav("Main navigation", "<ul><li>Home</li></ul>")
    .main_content("<h1>Welcome</h1><p>Content here.</p>")
    .aside("Related", "<p>Related links</p>")
    .build();
assert!(page.contains("<nav"));
assert!(page.contains("<main"));
assert!(page.contains("<aside"));

Implementations§

Source§

impl Template

Source

pub fn new() -> Self

Creates a new empty template.

§Examples
use html_generator::elements::Template;

let html = Template::new().build();
assert!(html.is_empty());
Source

pub fn nav(self, label: &str, content: &str) -> Self

Adds a navigation section.

§Examples
use html_generator::elements::Template;

let html = Template::new().nav("Primary", "<a href='/'>Home</a>").build();
assert!(html.contains("<nav"));
assert!(html.contains(r#"aria-label="Primary""#));
Source

pub fn header(self, content: &str) -> Self

Adds a header section.

§Examples
use html_generator::elements::Template;

let html = Template::new().header("<h1>Site</h1>").build();
assert!(html.contains("<header><h1>Site</h1></header>"));
Source

pub fn main_content(self, content: &str) -> Self

Adds the main content area.

§Examples
use html_generator::elements::Template;

let html = Template::new().main_content("<p>Body</p>").build();
assert!(html.contains("<main"));
assert!(html.contains(r#"role="main""#));
Source

pub fn section(self, label: &str, content: &str) -> Self

Adds a section to the template.

§Examples
use html_generator::elements::Template;

let html = Template::new().section("Intro", "<p>Hi</p>").build();
assert!(html.contains("<section"));
assert!(html.contains(r#"aria-label="Intro""#));
Source

pub fn aside(self, label: &str, content: &str) -> Self

Adds an aside section.

§Examples
use html_generator::elements::Template;

let html = Template::new().aside("Related", "<p>links</p>").build();
assert!(html.contains("<aside"));
Source

pub fn footer(self, content: &str) -> Self

Adds a footer section.

§Examples
use html_generator::elements::Template;

let html = Template::new().footer("&copy; 2026").build();
assert!(html.contains("<footer"));
assert!(html.contains(r#"role="contentinfo""#));
Source

pub fn build(&self) -> String

Renders the full template to an HTML string.

§Examples
use html_generator::elements::Template;

let html = Template::new()
    .nav("N", "<ul></ul>")
    .main_content("<p>x</p>")
    .build();
assert!(html.contains("<nav"));
assert!(html.contains("<main"));

Trait Implementations§

Source§

impl Clone for Template

Source§

fn clone(&self) -> Template

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Template

Source§

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

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

impl Default for Template

Source§

fn default() -> Template

Returns the “default value” for a type. 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, 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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more