Element

Struct Element 

Source
pub struct Element<E: HtmlElement> { /* private fields */ }
Expand description

A type-safe HTML element builder.

The type parameter E must implement [HtmlElement] and determines:

  • The tag name (via E::TAG)
  • Whether it’s a void element (via E::VOID)
  • Which children are allowed (via CanContain<Child> implementations)

Implementations§

Source§

impl<E: HtmlElement> Element<E>

Source

pub fn new() -> Self

Create a new empty element.

Source

pub fn attr( self, name: impl Into<Cow<'static, str>>, value: impl Into<String>, ) -> Self

Add an attribute with a string value.

Source

pub fn attr_value<V: AttributeValue>( self, name: impl Into<Cow<'static, str>>, value: V, ) -> Self

Add an attribute with a type-safe value.

Source

pub fn bool_attr(self, name: impl Into<Cow<'static, str>>) -> Self

Add a boolean attribute (no value, e.g., disabled, checked).

Source

pub fn class(self, class: impl Into<String>) -> Self

Add a class. Multiple calls append to the class list.

Source

pub fn id(self, id: impl Into<String>) -> Self

Add an id attribute.

Source

pub fn data(self, name: &str, value: impl Into<String>) -> Self

Add a data-* attribute.

Source

pub fn child<C, F>(self, f: F) -> Self
where E: CanContain<C>, C: HtmlElement, F: FnOnce(Element<C>) -> Element<C>,

Add a child element.

The child type must be allowed by the parent’s content model. This is enforced at compile time via the CanContain trait.

Source

pub fn text(self, content: impl Into<String>) -> Self
where E: CanContain<Text>,

Add text content.

Only available for elements that can contain text (via CanContain<Text>).

Source

pub fn raw(self, html: impl Into<String>) -> Self
where E: CanContain<Text>,

Add raw HTML content (not escaped).

Use with caution - this bypasses XSS protection.

Source

pub fn children<C, I, F>(self, items: I, f: F) -> Self
where E: CanContain<C>, C: HtmlElement, I: IntoIterator, F: Fn(I::Item, Element<C>) -> Element<C>,

Add multiple children from an iterator.

Source

pub fn when<F>(self, condition: bool, f: F) -> Self
where F: FnOnce(Self) -> Self,

Conditionally add content.

Source

pub fn when_else<F, G>(self, condition: bool, if_true: F, if_false: G) -> Self
where F: FnOnce(Self) -> Self, G: FnOnce(Self) -> Self,

Conditionally add content with else branch.

Source

pub fn into_node(self) -> TypedNode

Convert this element into a renderable node.

Source

pub fn render(&self) -> String

Render this element to a string.

Source

pub fn render_to(&self, output: &mut String)

Render this element to an existing string buffer.

Trait Implementations§

Source§

impl<E: Clone + HtmlElement> Clone for Element<E>

Source§

fn clone(&self) -> Element<E>

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<E: Debug + HtmlElement> Debug for Element<E>

Source§

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

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

impl<E: HtmlElement> Default for Element<E>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<E> Freeze for Element<E>

§

impl<E> RefUnwindSafe for Element<E>
where E: RefUnwindSafe,

§

impl<E> Send for Element<E>
where E: Send,

§

impl<E> Sync for Element<E>
where E: Sync,

§

impl<E> Unpin for Element<E>
where E: Unpin,

§

impl<E> UnwindSafe for Element<E>
where E: UnwindSafe,

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.