Hi, the weekend coder here!
I have a repetitive piece of html that displays nested data. It works just fine. I have no control over the depth of the nest therefore I need to translate this into a recursive function. The trouble is I cannot ‘see it’, as in, I don’t know how to carve the code between template and view. I know this is going to be a simple few lines of code!
The code is making use of detail and summary tags to make the nested structure expandable. When there are no children it just falls back to using divs, so there is a simple ‘if’ to look ahead for children. The expandable_body function sets the html elements and classes. There is a partial which holds the summary tags and includes a case statement that matches the actual data maps.
Any hints gratefully received.
<div class="mdc-layout-grid">
<div class="mdc-layout-grid__inner">
<div class="mdc-layout-grid__cell mdc-layout-grid__cell--span-3"></div>
<div class="mdc-layout-grid__cell mdc-layout-grid__cell--span-9">
<%= Enum.map(@legislation.body, fn el -> %>
<% children? = if length(el.children) > 0, do: :true, else: :false %>
<%= tag(expandable_body(children?).details.element, class: expandable_body(children?).details.class) %>
<%= render SponglWeb.LegGovUkView, "show_body_partial.html", element: el, children?: children? %>
<%= if children? do %>
<%= Enum.map(el.children, fn el2 -> %>
<% children2? = if length(el2.children) > 0, do: :true, else: :false %>
<%= tag(expandable_body(children2?).details.element, class: expandable_body(children2?).details.class) %>
<%= render SponglWeb.LegGovUkView, "show_body_partial.html", element: el2, children?: children2? %>
<%= if children2? do %>
<%= Enum.map(el2.children, fn el3 -> %>
<% children3? = if length(el3.children) > 0, do: :true, else: :false %>
<%= tag(expandable_body(children3?).details.element, class: expandable_body(children3?).details.class) %>
<%= render SponglWeb.LegGovUkView, "show_body_partial.html", element: el3, children?: children3? %>
<%= if children3? do %>
<%= Enum.map(el3.children, fn el4 -> %>
<% children4? = if length(el4.children) > 0, do: :true, else: :false %>
<%= tag(expandable_body(children4?).details.element, class: expandable_body(children4?).details.class) %>
<%= render SponglWeb.LegGovUkView, "show_body_partial.html", element: el4, children?: children4? %>
<%= if children4? do %>
<%= Enum.map(el4.children, fn el5 -> %>
<%= tag(expandable_body(:false).details.element, class: expandable_body(:false).details.class) %>
<%= render SponglWeb.LegGovUkView, "show_body_partial.html", element: el5, children?: :false %>
<%= tag(:"/#{expandable_body(:false).details.element}") %>
<% end) %>
<% end %>
<%= tag(:"/#{expandable_body(children4?).details.element}") %>
<% end) %>
<% end %>
<%= tag(:"/#{expandable_body(children3?).details.element}") %>
<% end) %>
<% end %>
<%= tag(:"/#{expandable_body(children2?).details.element}") %>
<% end) %>
<% end %>
<%= tag(:"/#{expandable_body(children?).details.element}") %>
<% end) %>
</div>
</div>
</div>