yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.44k stars 1.42k forks source link

Cannot use `for in` in html! macro #605

Closed itsgreggreg closed 5 years ago

itsgreggreg commented 5 years ago

Description

I'm submitting a ...

Cannot use for in in html! macro

        html! {
            <div>
            { for x in 1..10 {
                html!{
                  <p>{ format!("{}", x) }</p>
                }
              }
            }
            </div>
        }

Expected Results

<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p>

Actual Results

error: unexpected token
  --> src/lib.rs:24:20
   |
24 |             { for x in 1..10 {
   |                     ^^
error: aborting due to previous error

Context (Environment)

jstarry commented 5 years ago

Thanks for the issue! The parser issue is related to https://github.com/yewstack/yew/issues/606

As for your code sample, using a for loop is not a valid approach. I think you want something like this (untested):

html! {
  <div>
    { for (1..10).map(|x| html! { <p>{ format!("{}", x) }</p> }) }
  </div>
}

Once #606 is fixed, the for will not be necessary:

html! {
  <div>
    { (1..10).map(|x| html! { <p>{ format!("{}", x) }</p> }) }
  </div>
}
jstarry commented 5 years ago

Closing in favour of #606 but feel free to re-open if you have any more troubles!