kminevskiy

kminevskiy

Conditional rendering in LiveView template

Hey folks,

Does anyone know what could cause the following behavior: I have a live view template and in it I conditionally render a form (based on the value inside the socket assigns).

<%= if length(@users) > 0 do %>
...
<% end %>

When the length changes, it used to render a form, but now nothing happens. If I prepend that first line with, say, a tag <p><%= length(@users) %></p> Everything (showing the form dynamically based on the length of @users list) works as expected. I am pretty sure the code worked as is a few weeks ago before I updated to the latest Live View (but I’m not even sure it’s an issue with Live View), among other packages. Am I missing some new behavior introduced recently or is there something else I’m not doing correctly?

Most Liked

NobbZ

NobbZ

You shouldn’t to length like this. This will always iterate and count the full list.

Use something like match?([_|_], @users) instead.

PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

This might not help in this particular case, but I recently learned that a template gets rendered and send over the websocket connection every time the socket assigns are changed when it isn’t in the .leex format, so when it is a .eex file. Just changing all file formats to .leex reduced the websocket traffic by a lot! So, whenever you do <%= render "my_template.html", assigns %> inside a template, make sure that my_template is of the format .leex :slight_smile:

carterbryden

carterbryden

I had a similar issue with conditional rendering with liveview. I posted an issue but I just haven’t had enough time to create a reproduction repo publically (client project). In my case it seemed like it might be related to a nested liveview inside a conditional in a liveview template. Still don’t have a fix for it, I just changed the way I was rendering things to work around needing the if statement. I’ll post here if I do figure it out.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement