Skip to main content

options_from

Function options_from 

Source
pub fn options_from<K: AsRef<str>, V: AsRef<str>>(
    options: impl IntoIterator<Item = (K, V)>,
    selected: Option<&str>,
) -> Vec<Element>
Expand description

One <option value="…">label</option> per (value, label) pair, for Element::children_from.

The option whose value equals selected is marked selected; pass None when none is. Any iterator of pairs works, so a literal array and the rows of a query feed the same call. Values and labels are escaped on entry like every other attribute and text.

§Examples

use molt::prelude::*;
let rows = vec![("sp".to_string(), "São Paulo".to_string())];
let select = select_named("city")
    .children_from(options_from(rows.iter().map(|r| (&r.0, &r.1)), Some("sp")));
assert_eq!(
    select.render(),
    r#"<select name="city"><option value="sp" selected>São Paulo</option></select>"#
);