Flo0807
Hello everyone!
Phoenix LiveView v0.18 introduced the special attributes :let, :for and :if. In addition to the :if special attribute, I’d like to see an :else-like attribute.
The :if attribute is very useful for conditionally rendering blocks, but hiding a block is often accompanied by showing another block.
See the following example, which displays a message based on a user’s role:
<p :if={@role == :admin}>Hello Admin</p>
<p :if={@role != :admin}>Hello other roles!</p>
As you can see, the second :if is defined as the opposite of the first :if. This allows us to display a fallback block in case the user is not an admin. But showing a fallback block is a common use case, so let us add a special :else attribute that can be used to show such blocks:
<p :if={@role == :admin}>Hello Admin</p>
<p :else>Hello other roles!</p>
In the above example, the second block is being displayed when the :if in the first block evaluates to false.
This is not an entirely new idea. For example, Vue has a v-else directive and Svelte also has a {:else}.
Trending in Proposals: Ideas
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 8 of 8 Posts
krasenyp
Is the following that much worse in terms of looks?
I understand the pursuit of eye candy but we currently have a perfectly capable solution.
Flo0807
Thanks for pointing this out.
We also had a working solution before the
:ifand:forspecial attributes were added. As far as I know, the only reason those attributes were added was for syntax sugar, as the docs state:Therefore, an
:elseattribute would align with the:ifand:forattributes, even if its only purpose is to add some syntax sugar.Your example doesn’t look much worse, I think. Still, given both options, I’d choose the inline option, which is actually a bit cleaner.
sodapopcan
I’ve wanted this before but my general thoughts are:
<%= %>syntax.if/elsein templates, ie:elseis quite low compared to loneifs (this is, of course, just me)elsewould have to be aware of its immediate previous sibling and I don’t believe there is precedent for this (closest would be slots checking they are not in other slots). I can imagine this being deemed not worth taking on unless there was overwhelming demand for it.Having said all that if it were available I would use it, I just don’t personally want it that badly so have never bothered to bring it up myself.
Stefano1990
What makes HEEX different to Vue’s syntax is the fact that Vue doesn’t have the
<% %>so it needs to have :elsechristhekeele
I generally prefer this form. Part of the whole appeal of structured programming is that program structure dictates flow, so you do not have to scan through lines of code before knowing if they will be executed.
Conversely, the above is structure-less, and requires you check after reading any
:ifelement if the next element is an:elseto fully understand the output.LostKobrakai
Not 100%. You can use
:ifand:foron slots, but you cannot wrap slots in their eex counterparts.linusdm
I actually hit that subtle detail the other day, in the context of slots. It’s good to see it spelled out here.
rhcarvalho
I asked about this last year!
A year later, I find the older EEx form good for the clarity indentation brings, or extracting to components with pattern matching like the previous comments suggested sufficient for my needs.