Hentioe

Hentioe

Why does Phoenix 1.6 use html.heex as the suffix of the template file?

Because EEx is a universal template language, html.eex is used to indicate that this template is html content. Did I understand it correctly?

If so, I can’t understand why heex also uses this naming style. After all, heex means HTML EEx.

Marked As Solved

LostKobrakai

LostKobrakai

As @josemrb pointed to: The two extensions to the templates are for separate parts of the phoenix view layer.

The last part (usually heex, leex, eex, exs) selects the template encoder. It determines how the template content is turned into a function on the view module at compile time.

The middle part is for the content type, which is used at runtime for content negotiation, but also to select the format encoder. The format encoder turns the return value of the render function (maps for json, safe tuple for safe html, liveview rendered template struct) into actual iodata to send as response. This middle extension is also responsible so you can call the template by “index.html“.

Both of those parts are required for the view layer to work. The format encoders do not know anything about heex, given the the differences between heex and leex are purely handled at compile time and gone at runtime.

So while heex might be „html only“ the rest of the system is not at all aware of that.

Also Liked

dorgan

dorgan

It’s because of content negotiation *
With the .html.heex extension, phoenix will generate a render("foo.html", ...) clause in the view module. When rendering a page in the controller, passing :foo instead of "foo.html" or "foo.json" will make phoenix pick the correct clause based on the request headers.

The .html part of the name is used for content negotiation, and the .heex part is used to tell which compiler to use.

Content negotiation - HTTP | MDN (mozilla.org)
Phoenix.Controller — Phoenix v1.5.12 (hexdocs.pm) (rendering and layouts)
Phoenix.Controller — Phoenix v1.5.12 (hexdocs.pm) (render examples)

* That’s my conclusion, there may be other reasoning behind that

aziz

aziz

EEx is completely ignorant of the surrounding text. It might as well be binary data I think. HEEx actually parses the HTML tags and will raise an error if the syntax is incorrect. One great advantage is, for example, a more concise interpolation syntax in the attributes which is much more friendly to the eye:
<.form for={@user} id={@form_id} {@add_attrs}></.form>

aziz

aziz

Alright, let’s say the new Phoenix release had used the html.eex file ending. How would tools like editors, GUI Git tools etc. know what they’re actually dealing with? In an html.eex file attribute interpolations with braces wouldn’t mean anything. But if we have the particular extension heex then the tooling can understand that it’s dealing with something that’s more than EEx.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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

Other popular topics Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement