I imagine there is an easy way to accomplish this… I am attempting to do something like the below:
<% current_production_date = nil %>
<%= for widget <- @widgets do %>
<%= if (current_production_date != widget.production_date) do %>
<!-- ... output a new date header and re-assign current production_date -->
<% current_production_date = widget.production_date %>
<% end %>
<%= render "_widget.html", widget: widget %>
<% end %>
This won’t work as you can’t rebind the “current_production_date” variable outside of the comprehension… but this seems like a pretty common scenario, so I’m guessing there’s probably a straight-forward way of accomplishing something like the above… I just can’t figure out how… any advice much appreciated!
Thanks for the suggestions! I got things working with group_by/2. It is a little messy as I also needed to sort the resulting map but it seems to do the trick.
I ended up with something similar to:
<%= for {date, widgets}
<- Enum.group_by(@widgets, fn(x) -> DateTime.to_date(x.production_date) end)
|> Enum.sort(fn({date1, widget1}, {date2, widget2}) ->
case Date.compare(date2, date1) do
:lt -> true
_ -> false
end
end) do %>
<%= render "_date_header.html", date: date %>
<%= for widget <- widgets do %>
<%= render "_widget.html", widget: widget %>
<% end %>
<% end %>