John-B

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

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

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

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.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

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
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement