I was wondering what would be consider best practice when using shared view templates which generously uses the context helper functions.
Currently the way that I am going about this is to alias the context in the view module and using the functions directly in the view. For example:
View:
defmodule MyAppWeb.SharedView do
alias MyApp.Accounts
alias MyApp.Topics
...
end
Shared template:
<div>
<%= Accounts.get_user_name(user_id) %>
...
<%= Topics.get_topic(topic_id) %>
...
</div>
Context:
def get_user_name(%User{} = user) do
Repo.get(User, id).name
end
def get_topic(%Topic{} = topic) do
Repo.get(Topic, id)
end
I feel like wrapping the Ecto repo related functions in view functions seems kind of round about way of doing things when you have many Ecto repo calls and preloading the data in controller would also be a tedious thing to do when you want the shared template to exists on many different pages which would have different controllers.