pub struct Attribute<T>(pub T);
Expand description
Wrapper used for displaying attributes in elements
This wrapper can print attributes with or without values.
It can also handle attributes wrapped in an Option
and will behave accordingly.
§Examples
let html = another_html_builder::Buffer::default()
.node("div")
.attr("name-only")
.attr(("name", "value"))
.attr(Some(("other", "value")))
.attr(("with-number", 42))
.close()
.into_inner();
assert_eq!(
html,
"<div name-only name=\"value\" other=\"value\" with-number=\"42\" />"
);
§Extending
It’s possible to implement attributes with custom types, just by implementing the AttributeName and AttributeValue traits.
use std::fmt::Write;
struct ClassNames<'a>(&'a [&'static str]);
impl<'a> another_html_builder::AttributeValue for ClassNames<'a> {
fn render(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (index, inner) in self.0.iter().enumerate() {
if (index > 0) {
f.write_char(' ')?;
}
// this could be avoided if you consider it is escaped by default
another_html_builder::write_escaped_attribute_str(f, inner)?;
}
Ok(())
}
}
let html = another_html_builder::Buffer::default()
.node("div")
.attr(("class", ClassNames(&["foo", "bar"])))
.close()
.into_inner();
assert_eq!(html, "<div class=\"foo bar\" />");
Tuple Fields§
§0: T
Trait Implementations§
Source§impl<N: AttributeName, V: AttributeValue> Display for Attribute<(N, V)>
impl<N: AttributeName, V: AttributeValue> Display for Attribute<(N, V)>
Source§impl<N: AttributeName> Display for Attribute<N>
impl<N: AttributeName> Display for Attribute<N>
Source§impl<N: AttributeName, V: AttributeValue> Display for Attribute<Option<(N, V)>>
impl<N: AttributeName, V: AttributeValue> Display for Attribute<Option<(N, V)>>
Auto Trait Implementations§
impl<T> Freeze for Attribute<T>where
T: Freeze,
impl<T> RefUnwindSafe for Attribute<T>where
T: RefUnwindSafe,
impl<T> Send for Attribute<T>where
T: Send,
impl<T> Sync for Attribute<T>where
T: Sync,
impl<T> Unpin for Attribute<T>where
T: Unpin,
impl<T> UnwindSafe for Attribute<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more