Elixir: Loop through maps in a Phoenix template

There are many examples online of looping through lists in a Phoenix template, but I came across an instance where I needed to loop over a map. I wanted the same clean for items <- items do syntax. After a bit of research, I found this for loop is actually considered a comprehension.

When looping through a map, you can simply create a tuple with two arguments {key, value}.

<% collection = %{"List" => ["Item", "Item", "Item"]} %>
<%= for {key, list} <- collection do %>
<%= key %> # List
<%= for item <- list do %>
<%= item %> # Item
<% end %>
<% end %>

Magento 2: Using placeholders in form fields

While the Magento 2 adoption of KnockoutJS has opened up the possibility for modern front-end features, the level of abstraction has raised new challenges. Adding a placeholder to form fields is no longer as simple as opening the template file for the form. Here I will explain the steps for adding placeholders to Magento 2 fields.

Continue reading “Magento 2: Using placeholders in form fields”

Elixir: Using append and prepend options with inputs_for

The inputs_for method comes with two options for rendering the page with new elements as part of the collection you’re modifying (if it is a collection). For example, if you’re creating a to-do list that has a number of items, you could use the options like so:

<%= inputs_for f, :items, [append: [%App.Lists.Item{}], fn i -> %>
<%= text_input i, :description %>
<% end %>

If we already have items in the collection, inputs_for will iterate and fill the fields with the appropriate values, then append the new entry to the end, or in the case of prepend it will add the new entry to the beginning of the collection.