logo
pub fn maybe_attr<MSG>(
    name: &'static str,
    value: Option<impl Into<Value>>
) -> Attribute<&'static str, &'static str, AttributeValue<MSG>>
Expand description

Set the attribute of this element if value is Some, empty attribute otherwise

Examples

use sauron::prelude::*;

let width = Some(10);
let html: Node<()> = button(vec![maybe_attr("width", width)], vec![]);
let expected = r#"<button width="10"></button>"#;
assert_eq!(expected, html.render_to_string());

let width = None::<usize>;
let html: Node<()> = button(vec![maybe_attr("width", width)], vec![]);
let expected = r#"<button></button>"#;
assert_eq!(expected, html.render_to_string());