Gulliver
How can I change the default directory structure for templates in Phoenix app?
I just tried to create a new project and added some authentication to it by doing the “mix phx.gen.auth Accounts User users”. The result worked as expected but the directory/file structure it generated was quite a mess in my opinion. All the generated templates were directly under the “templates” directory, as user_registration, user_session and so on.
So I wanted to fix this by creating a “user” subdirectory and moving all the user related templates there but I couldn’t find a place where to tell phoenix where these templates files are. The generated view files were quite empty and they all just seem to call this “view” function. So where can I tell which templates this view actually renders and where they are?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
stefanchrobot
Take a look at the options taken by
use Phoenix.View.See here: Liveview conventions, Why put liveview template in live folder? - #7 by stefanchrobot
APB9785
The
viewfunction is inlib/my_app_web.ex:You can define other alternatives to
viewhere and use them instead when needed.tadasajon
I just refactored a project I’m working on to address precisely this issue. I added a new function to
myapp_web.ex, so now I have bothviewandaccounts_view:Then, I created a new
accounts/folder atlib/myapp_web/accounts/and I created three directories inside it:controllers/,views/,templates/. So my directory structure insidemyapp_weblooks like this:The controllers and the views can be moved into the new directories without affecting anything, but if the templates are moved then they will be in the wrong location – so I have edited each of the view files so it is an
:accounts_viewrather than a:view, following the adjustment I made tolib/myapp_web.ex. For example:Now I have everything having to do with user accounts under the
accounts/directory and out of the way.I’d appreciate any feedback, incidentally – the one thing I don’t like about this approach is the code-duplication between the
viewandaccounts_viewfunctions inlib/myapp_web.ex– I’d prefer to only change the root directory for the templates rather than copy-paste the whole function in the way that I did.stefanchrobot
Since I have both a server-side rendered app and an API for a mobile app in my app, I took it one step further and created separate
MyAppWebmodules, something along the lines of:and then:
Gulliver
Thanks, this does solve the issue. However, it seems like such an overkill to to change my whole code structure just to get some sanity in the directory sorting that I decided to get rid of the whole frontend part of Phoenix and only use it for the api and use React for the frontend. There’s seems to be bit too much magic happening with all of the rendering/templates part to my liking. Anyways, thanks for the help!