khaledh

khaledh

Domain-oriented web folder structure

I’m trying to find a way to organize my web folder into high-level application areas, and I’m starting with an admin area. However, the way I understand it, Phoenix encourages the following web folder structure:

hello_web
├── controllers
│   ├── foo_controller.ex
│   ├── bar_controller.ex
├── templates
│   ├── foo
│   │   ├── index.html.eex
│   │   ├── ...
│   ├── bar
│   │   ├── index.html.eex
│   │   ├── ...
├── views
│   ├── foo_view.ex
│   ├── bar_view.ex

while I’m trying to organize my folder structure like so:

hello_web
├── foo
│   ├── foo_controller.ex
│   ├── foo_view.ex
│   ├── templates
│   │   ├── index.html.eex
│   │   ├── ...
├── bar
│   ├── bar_controller.ex
│   ├── bar_view.ex
│   ├── templates
│   │   ├── index.html.eex
│   │   ├── ...

It seems a bit odd that on one hand Phoenix encourages domain-oriented organization of code into contexts (each with its own folder structure) in the main application folder, while on the other hand it encourages framework-oriented organization in the web folder.

What I tried so far is enforce the above folder organization, which seems OK up to the point when I try to override the template folder location per view. I don’t see a way to do this. The only option I found was in hello_web.ex, with the root: option to Phoenix.View:

use Phoenix.View, root: "lib/hello_web/templates"

but that seems to just change the root template folder for all views, and cannot be set on a view-by-view basis.

Marked As Solved

tme_317

tme_317

I’m doing the same thing. You can set it on a view-by-view basis. Just modify the view function in your hello_web.ex file so it starts like this:

  def view(opts \\ [root: "lib/hello_web/templates"]) do
    opts = opts ++ [namespace: HelloWeb]

    quote do
      use Phoenix.View, unquote(opts)

At the bottom of the file add a second macro to the one that is already there:

  defmacro __using__({which, opts}) when is_atom(which) do
    apply(__MODULE__, which, [opts])
  end

Now start every view where you want to override the template location with code like this:

defmodule HelloWeb.UserView do
  use HelloWeb, {:view, [root: "lib/hello_web/user/templates", path: ""]}

That should do the trick!

10
Post #2

Also Liked

tmbb

tmbb

I’ve written some phoenix generators (vphx for “vertical phoenix”) that generate a new app_web.ex file which supports this architecture and generates HTML that respects this folder organization. If people are interested I can release them somewhere. I still haven’t converted the phx.gen.auth generator into the new architecture (this generator is much more complex and generates many more files).

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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

We're in Beta

About us Mission Statement