elmx icon indicating copy to clipboard operation
elmx copied to clipboard

Nested Elmx

Open daig opened this issue 8 years ago • 1 comments

Hi, great library! Sometimes I'd like to nest Elmx syntax, for example when mapping over children:

<ul>{notes |> List.map (\ note -> <li>{view note}</li>)}</ul>

but this doesn't get the right result. Is there a fundamental barrier to nesting, or is it just extra tricky to implement?

daig avatar Oct 23 '16 19:10 daig

Hi @daig the short answer is that this is tricky to implement.

The detailed answer follows:

I'm currently using htmlparser2 to parse the Elmx (as HTML) into an AST and then from there generate the Elm code. The pro of this approach is that it keeps the elmx code small, the trade-off is that invalid HTML might break the parser and yield the wrong results (like in your example above).

Of course, this a sub-optimal implementation since not every Elmx needs to be a valid HTML, but it was a pragmatic decision (that we can review provided we find a better parser or an alternative approach).

For the time being, you can overcome this limitation by rewriting this:

<ul>{:notes |> List.map (\ note -> <li>{view note}</li>)}</ul>

like this:

let
  items = notes |> List.map (\ note -> <li>{view note}</li>)
in
  <ul>{:items}</ul>

Cheers!

pzavolinsky avatar Oct 25 '16 18:10 pzavolinsky