manuel-rubio

manuel-rubio

Rendering a slot but adding `assigns` previously, possible?

Hi people, I was playing a bit with Phoenix components and I just realised that the way I wanted to use the components is a bit tricky, I mean, I was reading the autogenerated code for the core_components.ex module, and I found that it’s using the internals of Phoenix.HTML but not the public functions, so I was trying:

  def simple_form(assigns) do
    ~H"""
    <.form :let={f} for={@for} class="form" {@rest}>
      <% assigns = assign(assigns, form: f) %>
      <%= render_slot(@inner_block) %>
      <hr/>
        <p class="help">(*) <%= gettext("required") %></p>
      <hr/>
      <div class="field is-grouped">
        <p class="control">
          <%= Phoenix.HTML.Form.submit @submit, class: "button is-primary" %>
        </p>
        <p class="control">
          <%= Phoenix.HTML.Link.link @cancel, to: @return_to, class: "button" %>
        </p>
      </div>
    </.form>
    """
  end

The main idea of the code is to provide for the elements inside of the render_slot the @form variable, but it’s not available and of course, the system is complaining because if I write:

<.simple_form for={@changeset} action={@action} return_to={@return_to}>
  <.checkbox name={:enabled} label={gettext("Enabled?")} help={gettext("If enabled, it will appear in the public listing of books.")}/>
</.simple_form>

The implementation is:

  def checkbox(assigns) do
    ~H"""
    <div class="field">
      <label class="label"><%= @label %></label>
      <div class="control">
        <label class={"switch is-rounded#{if(@form.errors[@name], do: " is-danger")}"}>
          <%= Phoenix.HTML.Form.checkbox @form, @name %>
          <span class="check"></span>
          <span class="control-label"></span>
        </label>
      </div>
    </div>
    """
  end

so, well, the @form isn’t available, is there a way to plug that from the simple_form render function or should I change my approach?

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement