Skip to content

Instantly share code, notes, and snippets.

@RichMorin
Last active November 20, 2018 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichMorin/d1d09ab0ef98d0219c90eb1a63fcc8de to your computer and use it in GitHub Desktop.
Save RichMorin/d1d09ab0ef98d0219c90eb1a63fcc8de to your computer and use it in GitHub Desktop.
concatenating the output of the link helper, take 2
I'm using a view function to create a heading and a list of links.
By way of context, here is the use case:
<%
import PhxHttpWeb.LayoutView
import RefData.Common
address = @item_data.address
types = ~w[ document email phone postal related web_site ]a
%>
<%= for type <- types do %>
<%= display(type, address) %>
<% end %>
Here is the (running, but fugly) code. FYI, keyss/1 returns a sorted list of keys
defmodule PhxHttpWeb.ResourcesView do
use PhxHttpWeb, :view
import RefData.Common
def display(type, address), do: display_h(type, address[type])
defp display_h(_, nil), do: ""
defp display_h(:web_site, submap) do
link_f = fn key ->
{:safe, iolist} = link(key, to: submap[key])
"<li>#{ iolist }</li>\n"
end
links = submap
|> keyss()
|> Enum.map(link_f)
"""
<h3>Web Site</h3>
<ul>
#{ links }
</ul>
"""
|> raw()
end
...
end
Any suggestions on making the code a bit cleaner?
-r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment