marcandre

marcandre

How to get `@view_module` from a layout with Phoenix.View 1.0.0

While Phoenix.View#render/3 used to set @view_module and @view_template for us to be used within the layout.

WIth the newer Phoenix.View 1.0.0, is there a way to access this information?

From the doc and the source, it looks like this has to be repeated explicitly now :crying_cat_face: anyone knows why?

# now we need some extra lines...
Phoenix.View.render(SomeModule, "some_template", assigns
|> Keyword.merge(
  view_module: SomeModule,
  view_template: "some_template"
))

Most Liked

LostKobrakai

LostKobrakai

In the template:

<title><%= title(assigns) %></title>

In the view module of the template:

def title(assigns) do
  module = Phoenix.Controller.view_module(assigns.conn)
  template = Phoenix.Controller.view_template(assigns.conn)
  module.title(template, assigns)
end

There’s no need to keep all that logic in the template itself.

josevalim

josevalim

Creator of Elixir

Those functions have been moved to Phoenix.Controller.view_module/view_template. The reason is because those concepts are not very clear (or very useful) in some abstractions like LiveView, so we realized it belongs upstream. I believe it was properly deprecated and there should be CHANGELOG entries. If you are not rendering from the controller, then you have to set it yourself.

marcandre

marcandre

Thanks. I wish I understood better, it still seems quite strange to me that an explicit call to Phoenix.View.render wouldn’t pass along the relevant information to the layout, and this seems quite remote from LiveView.

I opened Change wording on Changelog by marcandre · Pull Request #4571 · phoenixframework/phoenix · GitHub as the only reference in the Changelog was as a deprecation when this is actually a breaking change.

The upgrade guide is sadly a gist, so no PR possible. Could we consider moving it to the repo?

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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
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
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement