jmurphyweb

jmurphyweb

Calling View file functions from main app

Given that View modules contain formatting logic, what do you do if you want to format some data in your main application (ie in lib/myapp/ as opposed to in the lib/myapp_web/) ?

We are doing a load of CSV exports, and I keep finding myself aliasing View modules in my library modules which feels dirty.

How do you handle this? Do you have any self-enforced rules about which modules can call others? I’d love to hear about them!

Marked As Solved

mindok

mindok

Picking the right CSS class for given application state, conditionally showing username or “not logged in”, fiddling around with consistent CSS classes for inputs/buttons etc is something that I would put in a view module.

Also one-off stuff, like expanding a list in your assigns into a comma-separated list for display.

I see it as the final step to tidy things up so template code is more readable.

On the Phoenix docs overview page, the list is a little more terse than the description above:

  • render templates
  • act as a presentation layer
  • define helper functions, available in templates, to decorate data for presentation

Things like formatting phone numbers, addresses, currency etc are probably more business related. They can end up in email notifications, data exports, reports etc. They would most likely live in the app data modules (as per @lud suggestion) or in other modules depending on their complexity.

Ultimately here’s no hard and fast rules. One of the nice things about functional programming is it makes it relatively easy to move things around as your application grows or you discover module dependencies that no longer make sense. You put something into a function and a formatted thing comes out with no unpredictable side effects, so you can move that function elsewhere with relative ease.

Also Liked

dimitarvp

dimitarvp

Oops, sorry for the derp. :smiley:

In all Phoenix projects where I had any influence, I always moved such functions outside the _web namespace. They absolutely don’t belong there in my eyes. They are also not views per se, they are like data reshapers / transformers / whatever-you-want-to-call them.

So yep, move them to the other namespace and don’t have a care in the world. :slight_smile:

mindok

mindok

From the docs (https://hexdocs.pm/phoenix/views.html)

“Phoenix views have two main jobs. First and foremost, they render templates (this includes layouts). The core function involved in rendering, render/3 , is defined in Phoenix itself in the Phoenix.View module. Views also provide functions which take raw data and make it easier for templates to consume. If you are familiar with decorators or the facade pattern, this is similar.”

jmurphyweb

jmurphyweb

Thanks for the reply @dimitarvp

I am using view logic to format the data in the same way as we do in our templates.

Here is a trivial example:

# in controller
def csv_users(conn, params) do
  binary = Accounts.buid_users_csv_export()
  send_download(conn, binary: binary)
end

# in Accounts.Users.ExportCSV
def buid_users_csv_export() do
    Users.list_users() 
    |> Enum.map(fn user ->
      [
        UserView.format_name(user),
        UserView.format_address(user),
      ...
      ]
     |> NimbleCSV.build_csv()
end

This is a really basic example of pseudo code but it is exactly the issue that I am facing in many places in my application. In reality, there is too much formatting to just redefine the functions locally to this module.

Any help really appreciated :pray:

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 13924 124
New
IVR
Hi all, I’ve seen a number of related threads in the past, but I’d still be very curious to hear an up-to-date opinion on this topic. I...
New
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement