Last active
October 4, 2015 18:31
-
-
Save BillBarnhill/0a5ec2036a5279965b76 to your computer and use it in GitHub Desktop.
My snippet for generation of tag list using PhoenixHTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp tag_to_link(conn, tag) do | |
{:safe, body} = link to: Router.Helpers.tag_path(conn, :show, tag.id) do | |
tag.name | |
end | |
body | |
end | |
@doc """ | |
Produces a comma-separated list of links to tag pages for tags | |
in a post (commas are TBD) | |
## Example | |
Given a post with two tags, Foo (1) and Bar (2), in a template this... | |
<%= tag_link_list(@conn, @post) %> | |
should produce this... | |
<div class="taglist"><a href="/tag/1">Foo</a><a href="/tag/2">Bar</a></div> | |
currently produces this: | |
<div do="<a href="/tag/1">Meta</a><a href="/tag/2">Communitivity</a>"> | |
""" | |
def tag_link_list(conn, post) do | |
tags = Repo.all( from t in Tagging, where: t.post_id == ^post.id, preload: [:tag]) |> Enum.map(&(&1.tag)) | |
links = Enum.map(tags, fn (t) -> tag_to_link(conn, t) end) | |
#flattened = List.flatten(links) | |
#binned = List | |
binned = :erlang.iolist_to_binary(links) | |
tag(:div) do | |
binned | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment