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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews