lambda-fairy / maud

:pencil: Compile-time HTML templates for Rust
https://maud.lambda.xyz
Apache License 2.0
2.09k stars 137 forks source link

Support adding attributes through iterators #386

Open jquesada2016 opened 1 year ago

jquesada2016 commented 1 year ago

Please foregive me if this is already possible, as I did not find it in the book. But I am trying to componentize some markup, and need to support adding arbitrary attributes. So for example, let's take an <input>

struct Input {
  attrs: Vec<(String, String)>
}

impl Render for Input {
  fn render(&self) -> Markup {
    html! {
      input type="text" ...self.attrs;
    }
  }
}

Is something like this currently possible? I understand the security implications, but this is intended to be done at compile time, by adding attributes in different parts of the app, not dynamically.

jquesada2016 commented 1 year ago

If not currently possible, perhaps iterators and a spread operator might be good additions to the syntax?

Attribute spreading needs to implement Iterator<Item = (impl Display, impl Display)> and could use the rust spread operator..while classes can be spread via. ..or something similar? Would only need to implementIterator`