|
15 | 15 | //! All text and attribute values are automatically escaped to ensure safe and valid HTML output. |
16 | 16 | //! |
17 | 17 | //! ``` |
18 | | -//! use fun_html::{attr::class, elt::h1}; |
| 18 | +//! use fun_html::{attr, elt}; |
19 | 19 | //! |
20 | | -//! let greeting = h1( |
21 | | -//! [class(["bold"])], // <-- attributes |
| 20 | +//! let greeting = elt::h1( |
| 21 | +//! [attr::class(["bold"])], // <-- attributes |
22 | 22 | //! ["Hello world!".into()], // <-- children |
23 | 23 | //! ); |
24 | 24 | //! assert_eq!(greeting.to_string(), r#"<h1 class="bold">Hello world!</h1>"#); |
|
27 | 27 | //! Because those are simple rust functions, it is easy to leverage rust features like conditions, loops and iterators: |
28 | 28 | //! |
29 | 29 | //! ``` |
30 | | -//! # use fun_html::{elt::{li,ul,text}}; |
| 30 | +//! use fun_html::{elt, Element}; |
31 | 31 | //! |
32 | | -//! let list_values = true; |
33 | | -//! let element = if list_values { |
34 | | -//! ul([], (1..=3).map(|n| li([], [n.to_string().into()]))) |
35 | | -//! } else { |
36 | | -//! text("no value") |
| 32 | +//! fn list_view(items: Vec<i32>) -> Element { |
| 33 | +//! if !items.is_empty(){ |
| 34 | +//! elt::ul([], items.into_iter().map(|item| elt::li([], [item.to_string().into()]))) |
| 35 | +//! } else { |
| 36 | +//! elt::text("no item") |
| 37 | +//! } |
37 | 38 | //! }; |
38 | 39 | //! |
39 | | -//! assert_eq!(element.to_string(), "<ul><li>1</li><li>2</li><li>3</li></ul>") |
| 40 | +//! assert_eq!( |
| 41 | +//! list_view((1..=3).collect()).to_string(), |
| 42 | +//! "<ul><li>1</li><li>2</li><li>3</li></ul>" |
| 43 | +//! ); |
40 | 44 | //! ``` |
41 | 45 | //! |
42 | 46 | //! ## Escape hatches |
|
0 commit comments