Do you think that the View can access the context, or that only the Controller should fetch the data from the context and put it in conn.assigns
(and template passes data to the View) ?
Imagine the following example: we want to display a form to create a new post in our blog. In this form, there should be a select
element allowing to select the category the post belongs to.
The View provides a helper category_select_options
for the template, returning the data for the select
element.
1. The View accesses the context:
defmodule MyAppWeb.PostView do
use MyAppWeb, :view
def category_select_options() do
categories = Blog.list_categories() # the View fetches data from the Context
for category <- categories,
do: {
category.name
category.id
}
end
end
The template calls the function without any argument:
<%= select f, :category_id, category_select_options() %>
2. The Controller fetches and assigns the data in the conn
; the template passes the data to the View
defmodule MyAppWeb.PostController do
use MyAppWeb, :controller
alias MyApp.Blog
plug :assign_categories when action in [:new, :create, :edit, :update]
defp assign_categories(conn, _) do
assign(conn, :categories, Blog.list_categories())
end
# actions' code
end
defmodule MyAppWeb.PostView do
use MyAppWeb, :view
def category_select_options(categories) do
for category <- categories,
do: {
category.name
category.id
}
end
end
The template passes the @categories
from conn.assigns
to the View, which prepares the data for the select
element:
<%= select f, :category_id, category_select_options(@categories) %>
Which do you think is better and most importantly why?