-
I'm trying to do exactly what this example is saying with // This also has the benefit of being more flexible: because these are all plain Rust functions and methods, it’s easier to use them in things like iterator adapters without any additional “magic”:
// take some set of attribute names and values
let attrs: Vec<(&str, AttributeValue)> = todo!();
// you can use the builder syntax to “spread” these onto the
// element in a way that’s not possible with the view macro
let p = attrs
.into_iter()
.fold(p(), |el, (name, value)| el.attr(name, value)); But since use leptos::prelude::*;
use leptos::html;
fn build_element(classes: &Vec<String>) -> impl IntoView {
classes
.iter()
.fold(html::p(), |elem, cls| elem.attr("class", cls))
.into_view()
}
error[E0308]: mismatched types
--> src/main.rs
|
153 | .fold(html::p(), |elem, cls| elem.attr("class", cls))
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `HtmlElement<P, (), ()>`, found `HtmlElement<P, (...,), ...>`
|
= note: expected struct `leptos::html::HtmlElement<_, (), _>`
found struct `leptos::html::HtmlElement<_, (CustomAttr<&str, &std::string::String>,), _>` Any ways to append attributes to an HtmlElement without changing the For the reference, I'm trying to port leptos-markdown to the latest leptos version. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I am not sure who maintains that fork/translation of the book or where it diverged from main, but it's not the current version of that page. If I were writing your fn build_element(classes: &Vec<String>) -> impl IntoView {
let class = classes.join(" ");
html::p().class(class)
} (Note that your example would compile but not work as expected in previous versions, so not compiling is probably an improvement — repeatedly setting the |
Beta Was this translation helpful? Give feedback.
Sure, you can implement a function like this -- it's mostly just that some of the builder patterns have changed to accommodate the type system, so the changes here have been relatively bigger than in typical code. But here's a version of the function above that compiles