nesimtunc

nesimtunc

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 :slight_smile:

Thank you in advance!

Showing Posts 1 to 10

lubien

lubien

How about:

~H"""
<dd class="descp" lang={lang_propery_for_lang_code(v.lang_code)}><i><%= v.descp %></i>
</dd>
"""

and on your core components:

def lang_propery_for_lang_code("a"), do: "b"
def lang_propery_for_lang_code(_other), do: "c"
aiwaiwa

aiwaiwa

So content={if true, do: "this", else: "that"} does not work for you?

nesimtunc

nesimtunc OP

Can we do this in HTML template file?

nesimtunc

nesimtunc OP

I think I could manage like this:

<ul id='languages_{if @mobile, do: "mobile", else: ""}' class="dropdown-content">

However due to conditional HTML tag the compiler thinks it’s not closed, so, it won’t compiler at the moment.

<%= if @mobile do %>
    <ul id="nav-mobile" class="sidenav" style="transform: translateX(-105%);">
<% else %>
    <ul class="right hide-on-med-and-down">
<% end %>
        <li>
            <a class='dropdown-trigger' href='#' 
            data-target='languages_{if @mobile, do: "mobile", else: ""}'>
                <i class="material-icons right">language</i>
            </a>
        </li>
    </ul>
<ul id='languages_{if @mobile, do: "mobile", else: ""}' class="dropdown-content">
    <li><a href="/" lang="a">A</a></li>
    <li><a href="/tr" lang="b">B</a></li>
    <li><a href="/en" lang="c">C</a></li>
</ul>

Error:

== Compilation error in file lib/ferhengco_web/views/layout_view.ex ==
** (Phoenix.LiveView.HTMLTokenizer.ParseError) lib/myapp_web/templates/layout/_menu.html.heex:12:5: missing opening tag for

We know it will be open and close eventually when it’s rendered, but why compiler can’t understand this? Previously, before upgrade to live view it was working fine like this.

I’m thinking to move a li item to a variable and use in both case within <ul>@{langs}</ul>

Is there a better way to do this?

chrismccord

chrismccord

Creator of Phoenix

You need to use {} for the dynamic expression like @lubien first showed:

<ul id={"languages_#{if @mobile, do: "mobile", else: "..."}"} class="dropdown-content">
nesimtunc

nesimtunc OP

I must tell @chrismccord sensei but I don’t like this syntax :slight_smile: Thank you!

I’m able to compile only this way, I get this visual error in VSCode, not sure which extension is showing this though.

Single Quote

Doube Quote

LostKobrakai

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

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

nesimtunc

nesimtunc OP

This way is much better and it works!

<ul id={if @mobile, do: "nav-mobile"} 
 class={if @mobile, do: "sidenav", else: "right hide-on-med-and-down"}
 style={if @mobile, do: "transform: translateX(-105%);"}>

But with same plug-in error. Which plug-in do you use? I’m using these ones:



aiwaiwa

aiwaiwa

.vscode/settings.json:

{
	"files.associations": {
		"*.heex": "phoenix-heex",
	},
	"[elixir]": {
		"editor.formatOnSave": true,
		"editor.defaultFormatter": "JakeBecker.elixir-ls"
	},
	"[phoenix-heex]": {
		"editor.formatOnSave": true,
		"editor.defaultFormatter": "JakeBecker.elixir-ls"
	},
	"emmet.includeLanguages": {
		"phoenix-heex": "html",
		"elixir": "html",
	},

Some great hex plugins:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews