nesimtunc
Ternary if in new HEEx Live Template
Hello everyone,
I’m seeking help in converting the following HTML code snippets into the new HEEx template format:
<dd class="descp" lang='<%= (if v.lang_code == "a", do: "b", else: "c")%>'><i><%= v.descp %></i>
</dd>
<meta property="al:ios:url" content='myapp://<%= if(@conn.assigns[:query], do: "w/" <> @conn.assigns[:query], else: "") %>'/>
In old version we used to do like this:
<meta name="keywords" content='bla bla <%= if(@conn.assigns[:query], do: @conn.assigns[:query], else: "") %>'/>
But now I can do this like this according to the doc, but it doesn’t seem convenient for me.
<% keywords = "bla bla" %>
<% if @conn.assigns[:query] do %>
<% keywords = keywords <> @conn.assigns[:query] %>
<% end %>
<meta name="keywords" content='@{keywords}'/>
So, tell me the best practice please ![]()
Thank you in advance!
Marked As Solved
Also Liked
LostKobrakai
<ul id={if(@mobile, do: "nav-mobile")} …>
You don’t need to wrap an outer condition in a string. Within attr={…} any elixir expression is allowed. id={nil} won’t render the attribute, but won’t cause any issues, so you can also skip the else part of the if.
aiwaiwa
So content={if true, do: "this", else: "that"} does not work for you?
aiwaiwa
Feels like you have some mismatched parenthesis somewhere. May be outdated plugin

Which, as @LostKobrakai points out, can be simplified as interpolation is indeed unnecessary (in first case)

But both work for sure







