Sanjibukai

Sanjibukai

Hello everybody,
I’m trying to figure out where the default “app.html.eex” file is defined in all the pipeline.
The better I managed to find is the following explanation on the Phoenix Controller part of the documentation:

defmodule MyAppWeb.UserController do
 use Phoenix.Controller
 def show(conn, _params) do
   render(conn, "show.html", message: "Hello")
 end
end

will render the “show.html” template inside an “app.html” template specified in MyAppWeb.LayoutView .

However I wasn’t able to find the exact part in the source code where this is happening.
I tried by renaming the file (for example ap.html.eex which it gives me an error) to follow the stack trace to find where the default layout is applied.

I also tried to figure out where the app.html.eex template is injected by putting many IO.inpsect(template) all over the source code of Phoenix around the renders functions where things seem to happen.. But without success..

I also read in Chris’ book (Programming Phoenix) the following (on page 57):

When you call render in your controller, you’re actu-
ally rendering with the :layout option set by default. This allows you to render
the view and template for your controller action in the layout with a plain
render function call. No magic is happening here.

To be honest it is like magic for me..

Any details will be appreciated!
Thank you..

Showing Posts 1 to 3

Kurisu

Kurisu

I found this in deps/phoenix/lib/phoenix/controller.ex __using__ macro definition:

      plug :put_new_layout, {Phoenix.Controller.__layout__(__MODULE__, opts), :app}
      plug :put_new_view, Phoenix.Controller.__view__(__MODULE__)

And in the same file (there is LayoutView at the last line ):

 @doc false
  def __layout__(controller_module, opts) do
    namespace =
      if given = Keyword.get(opts, :namespace) do
        given
      else
        controller_module
        |> Atom.to_string()
        |> String.split(".")
        |> Enum.drop(-1)
        |> Enum.take(2)
        |> Module.concat()
      end
    Module.concat(namespace, "LayoutView")
  end
Sanjibukai

Sanjibukai OP

Thanks I didn’t think to look at __using__ and also didn’t think to look for the atom :app..

Btw, does anyone know the reason behind the double underscore formatting for __layout__ and __view__?
I got that for __MODULE__it’s a language construct for identifying the current module, but what about using this format for defined functions?

Kurisu

Kurisu

This is in Elixir naming convention:

Underscore ( _foo )

(…)

Function names may also start with an underscore. Such functions are never imported by default:

iex> defmodule Example do
...>   def _wont_be_imported do
...>     :oops
...>   end
...> end

iex> import Example
iex> _wont_be_imported()
** (CompileError) iex:1: undefined function _wont_be_imported/0

Due to this property, Elixir relies on functions starting with underscore to attach compile-time metadata to modules. Such functions are most often in the __foo__ format. For example, every module in Elixir has an __info__/1 function:

iex> String.__info__(:functions)
[at: 2, capitalize: 1, chunk: 2, ...]

Elixir also includes five special forms that follow the double underscore format: __CALLER__/0 , __DIR__/0 , __ENV__/0 and __MODULE__/0 retrieve compile-time information about the current environment, while __STACKTRACE__/0 retrieves the stacktrace for the current exception.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews