John-B
Phoenix namespacing and views/templates questions
I created a Phoenix application called ‘hello’. My question is in router.ex
defmodule HelloWeb.Router do
does HelloWeb have anything to do with namespacing or parent directory? Or it can be something else? Same with
defmodule HelloWeb.HelloController do
defmodule HelloWeb.HelloView do
or are they just convention?
Also why do we need a view even if empty to tie controllers to templates in Phoenix?
Thanks for the help!
Most Liked
LostKobrakai
There’s not much to it in phoenix. The only implicit generation of module names I’m aware of is that a controller will use the view of the same name just where Controller is replaced with View. If you don’t want it implicitly generated you can always use conn |> put_view(view) |> render(template, assigns) in your controller actions (or put the view earlier). The other instance of where a module name prescribes some other name is for the folder where a view takes its templates from, but this part only ever affects a single view at a time and is also configurable.
NobbZ
The namespace of elixir is flat, “namespacing” is just a convention.
Also location in the filesystem or even the name of the file does not matter at all, as long as the compiler can find it.
Because during compiletime the templates get compiled into the view module as functions.
Kurisu
I’m not native english speaker but “required” seems too strong for me.
I think for practical purposes as you said, you can choose your own module namespace as long as you ensure its uniqueness and the SomeModule format.
For example by default you have HelloWeb.PageController which is located to hello_web/controllers/page_controller.ex. One could name it HelloWeb.Controllers.PageController or ever HelloWeb.Controllers.Page, but I think the default name is nicer… ^^
And one last example, I like to create for example a folder named project_path/lib/hello/extras and name its modules whatever I want as long as I like it, such as Site, Easy… I do that for extra modules I want to call anywhere in the project but with very short namespace, without the Hello namespace part. ^^
All that to say don’t feel forced to keep HelloWeb.
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









