Created
October 4, 2015 18:40
-
-
Save BillBarnhill/4fb32d7342d61f6497dc to your computer and use it in GitHub Desktop.
My fixed 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> | |
Now that I'm not using tag function it works, though is ugly | |
""" | |
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 = ["<div>",Enum.map(tags, fn (t) -> tag_to_link(conn, t) end), "</div>"] | |
{:safe, links} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment