neuone

neuone

Creating a comma separated list of tags

I have an template that is creating tags that are links.
I would like to add commas between the tags.
How can I do this so that the last tag does not have a comma.

<%= for tag <- @video.tags do %>
    <%= link tag.name , to: tag_path(@conn, :show, tag.name) %>,
 <% end %>

Current Output
family, friends, vacation,

Desired Output
family, friends, vacation

Marked As Solved

LostKobrakai

LostKobrakai

# in the view module
def format_tags(conn, tags) do
  tags
  |> Enum.map(fn tag -> link(tag.name, to: tag_path(conn, :show, tag.name) end)
  |> Enum.intersperse(", ")
end

# in the template
<%= format_tags(@conn, @video.tags) %>

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

And if you actually only want the comma’s to show to users but not in the HTML source (i.e. it is not interesting to SEO bots):

<div class="video_tags">
<%= for tag <- @video.tags do %>
    <%= link tag.name , to: tag_path(@conn, :show, tag.name) %>,
 <% end %>
</div>
.video_tags > a::after {
    content: ", ";
    display: inline;
}
.video_tags > a:last-child::after {
    display: none;
}

Example

neuone

neuone

I didn’t know you can do this with just CSS. Nice tip :+1:

neuone

neuone

I’m starting to understanding how you can use the view module now.
You can create your list of html tags inside it and then use it in the template. Makes sense.

I always wanted to know what “Enum.intersperse” did. Cool example.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement