laiboonh

laiboonh OP

exactly :smile:

0
Post #11
Chrichton

Chrichton

Hi all.

I am missing an else option.

I have a form_component to render new and existing categories.
Do I have to write is this way?

 <.simple_form
        for={@form}
        id={"category-form-#{@category_id}"}
        phx-submit="save"
        phx-change="validate"
        phx-target={@myself}
      >
        <h1 :if={@category_id}>{gettext("Edit Category")}</h1>
        <h1 :if={@category_id == nil}>{gettext("New Category")}</h1>
        <.input field={@form[:name]} label={gettext("Name")} />
        <.input field={@form[:description]} label={gettext("Desription")} type="textarea" />

        <:actions>
          <.button>{gettext("Submit")}</.button>
        </:actions>
      </.simple_form>

or is there a more elegant solution?

Thinks for your reply, Heiko

0
Post #12
olivermt

olivermt

There is no shorthand for :else

But you can add an else like this I think?
(Not tested, am on phone)

<%= if @some_condition do %>
  <div>...</div>
<%= else do %>
  <p>…</p>
<% end %>
0
Post #13
kokolegorille

kokolegorille

It is possible to use if/else, but with else like this

<% else %>

0
Post #14
sodapopcan

sodapopcan

Annnnd just for posterity since they are on latest:

{if @some_condition do}
  ...
{else}
  ...
{end}
2
Post #15
kokolegorille

kokolegorille

I prefer the new syntax, it looks like … javascript :slight_smile:

0
Post #16
sodapopcan

sodapopcan

Ha, I just prefer without its JS connotations :upside_down_face: Though I’m no JS-hater.

0
Post #17
LostKobrakai

LostKobrakai

Block level constructs still require <%= %>.

1
Post #18
sodapopcan

sodapopcan

Whoa wha… damn, sorry for the misinformation. I could swear I’ve done this! But apparently not. Dang. I’m having a Mandela moment here.

0
Post #19
codeanpeace

codeanpeace

If you take a look at the form component generated by mix phx.gen.live, it sets a @page_title assign instead of handling the conditional logic in the heex template.

If you want to keep it in the template, here are some other options:

# `if/2` with a one liner keyword list, balances clarity and conciseness, my pick of the bunch
<h1>{if @category_id, do: gettext("Edit Category"), else: gettext("New Category")}</h1>
# pseudo-ternary, very concise but arguably too clever, also unexpected gotcha if middle bit is false-y when conditional is truth-y 
<h1>{@category_id && gettext("Edit Category") || gettext("New Category")}</h1>
# not my personal preference, but for the sake of completeness 
<h1 :if={@category_id}>{gettext("Edit Category")}</h1>
<h1 :if={@category_id == nil}>{gettext("New Category")}</h1> # or
<h1 :if={is_nil(@category_id)}>{gettext("New Category")}</h1> # or
<h1 :if={!@category_id}>{gettext("New Category")}</h1>
1
Post #20