In more than one occasion there’s a tag with an :if attribute immediately followed by a sibling tag with another :if that is the negation of the previous condition.
I wonder what other people are doing for cases like that? Repeating+negating conditions feels error prone to me.
How I think it should be written
I think <%= if %> ... <% else %> ... <% end %> would be more appropriate for template code like above? Or even <%= case %> and <%= cond %> for the more general cases?
I know JS frameworks like Vue have <tag v-if="condition">...</tag><tag v-else>...</tag>, but would not propose making Heex have even more ways to accomplish things that are already possible with existing syntax
I just got an idea. Why not create one’s own component named if or so.
It could be used like below.
<.if true={true}>
This is true!
<:else>
That's false!
</:else>
</.if>
So basically we have the if component which has a true attribute to check a condition and an else slot to render something when the condition passed to true won’t met. Otherwise the content in the inner_block will be the one to be rendered.
Still, which previous :if would this :unless correspond to? I ask because I think what the OP is looking for is a way of not having to check a condition when a previous tag has already checked the negation of that same condition.
Then perhaps you’re saying that any :unless that follows a :if at the same nesting level should be considered the negation of that :if? In that case, the :unless wouldn’t even need to specify a condition.
:unless={condition} would be a short hand for :if={!condition} , no previous clause needed. I’m only suggesting it because elixir already has it: Kernel — Elixir v1.16.0-rc.0 (just not in heex).
Yeah, I’m just more certain I would not like to learn more syntax (and inflict the same learning on everyone else) and have so many ways to accomplish the same things
Yes, it is. And to this day the :if attribute doesn’t have an else counterpart, which is okay. My OP was trying to understand why people would use :if={cond} immediately followed by :if={!cond}, instead of using the older {%= if cond do %}...{% else %}...{% end %}.
I guess it’s just a matter of personal preference, sensibilities, style. No deep revelation