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
# 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) %>
8
Also Liked
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;
}
7
neuone
I didn’t know you can do this with just CSS. Nice tip ![]()
2
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.
2
Popular in Questions
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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
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
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
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
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Other popular topics
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
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...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
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
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









