laiboonh
Out of curiousity how does conditional rendering :if heex attribute work? I look into the web console logs and see that if condition is true the static content is passed over to the browser in s: [...]. But whenever the condition is false how does the browser know to remove the static content?
Is there something in the liveview javascript that handles this for us behind the scene?
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
milangupta
The processing is happening on the server side ..
The beauty of this is security ..
LostKobrakai
The merging of what’s in the DOM vs what is supposed to be in the DOM is handled by morphdom in the browser. The phoenix liveview javascript does all the reconsiliation of dynamic and static parts, but from there the template is passed to morphdom for applying any changes to the DOM.
milangupta
Benjamin - so I understand this precisely too .. can you explain this a bit simpler .. what CPU is processing the :if ? Is it the server or the client (browser) ?
My interpretation was that the conditional processing etc. (heex template => html/css) is done on the server side ? Do I have it wrong ?
LostKobrakai
That’s not completely correct. The
:ifcondition is indeed checked on the server, but there isn’t plain html sent to the client. There’s an optimized diff sent to client, which either includes content or doesn’t. There’s more code on the client to figure out what that diff means, and the result is applied by morphdom, which takes some html and updates the DOM to match that html.milangupta
I understand/know the morphdom and diff management .. however, one of the strengths of elixir/phoenix vs. javascript that I thought was a big plus was that conditional code isn’t pushed to the client side. So, in effect, the client browser would never ever see what is behind the
> <div :if={false}> **secret stuff** </div>.. that isn’t sent over the websocket .. right ?
With javascript, your code is basically distributed to and running on the browser (I know I am oversimplifying .. ), free to be inspected by anyone ..
codeanpeace
My current mental model of how conditional rendering works in LiveView:
When the
:ifcondition is false by default, then browser won’t receive the content guarded by the:if. Then if the:ifcondition subsequently switches from false to true, the server sends an optimized diff representing the addition of that guarded content to the client which updates the DOM via morphdom.When the
:ifcondition is true by default, then browser will receive the content guarded by the:if. Then if the:ifcondition subsequently switches from true to false, the server sends an optimized diff representing the removal of that guarded content to the client which updates the DOM via morphdom.LostKobrakai
Exactly.
I think this is what the OP was asking about. The diff doesn’t contain any content and also doesn’t seem to contain any instructions for deleting content, so something on the client needs to be able to understand that such a diff means “removal of what is there”.
codeanpeace
As much as I enjoy Elixir/Phoenix, that’s a server side rendering advantage and not language/ecosystem specific.
Right, because the
:ifcondition defaults to false, your secret stuff is safe so long as that condition never turns true. There’s a caveat though, it’s pretty common to set the:ifcondition to an assign and when doing so, it is important to note that if there’s an unguardedhandle_event/infodefined that toggles that assign, a malicious user could reveal your secret stuff.milangupta
Thanks for explaining it .. I think you nailed it for me.
I had not yet understood the scenario where the conditional was based on an assign. Makes sense ..
Kind regards.
codeanpeace
You’re welcome! For posterity’s sake, here’s a simple naive example of what I meant by the caveat above.
To learn more about this and the importance of authorization/permission when it comes to LiveView events, check out this guide on the Security considerations of the LiveView model.