Question about Phoenix and EEx

63 views
Skip to first unread message

Luiz Paulo Vasconcellos

unread,
Jul 12, 2016, 8:56:40 AM7/12/16
to elixir-lang-talk
I'm reading through Phoenix's source code in order to familiarize myself more with the framework and elixir itself.
One thing I can't seem to understand is the rendering process. How are phoenix View helper functions made available in templates? I initially thought it inserted something like <% import MyView %> in each template before compiling, but it doesn't seem so.

I'm still in the beginning of the learning journey, sorry if this is an obvious question. Thanks!

Michał Muskała

unread,
Jul 12, 2016, 9:14:18 AM7/12/16
to elixir-l...@googlegroups.com
The views are compiled to just being functions in the view module. So exactly how you can use other functions from a module in a function
in tha module, you can use other functions from the view module in templates.

Michał.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/f32cdd6d-1213-4ae8-86c0-abdb9c0b5be0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Luiz Paulo Vasconcellos

unread,
Jul 12, 2016, 9:19:19 AM7/12/16
to elixir-lang-talk
That makes sense, thanks for the quick answer.

Rich Morin

unread,
Jul 12, 2016, 12:25:42 PM7/12/16
to elixir-l...@googlegroups.com
> On Jul 12, 2016, at 06:14, Michał Muskała <mic...@muskala.eu> wrote:
> The views are compiled to just being functions in the view module. So exactly how you can use other functions from a module in a function
> in tha module, you can use other functions from the view module in templates.

I suspect there's an easy way to display one of these compiled functions;
might you know this?

-r

--
http://www.cfcl.com/rdm Rich Morin r...@cfcl.com
http://www.cfcl.com/rdm/resume San Bruno, CA, USA +1 650-873-7841

Software system design, development, and documentation


OvermindDL1

unread,
Jul 12, 2016, 1:27:50 PM7/12/16
to elixir-lang-talk
```elixir
iex(1)> MyServer.IndexView.module_info[:exports]
[__info__: 1, __phoenix_recompile__?: 0, __resource__: 0, __templates__: 0,
 breadcrumb: 4, menu: 4, render: 1, render: 2, template_not_found: 2,
 module_info: 0, module_info: 1]
```

It generated the `render/1/2` and `template_not_found/2` functions.  The code that does starts at:  https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/template.ex#L136

And you can follow it from there.  :-)

OvermindDL1

unread,
Jul 12, 2016, 1:32:55 PM7/12/16
to elixir-lang-talk
Oh and yes, the actual EEX compilation happens at:  https://github.com/elixir-lang/elixir/blob/v1.3.1/lib/eex/lib/eex.ex#L150

Luiz Paulo Vasconcellos

unread,
Jul 12, 2016, 1:40:29 PM7/12/16
to elixir-lang-talk
From what I found in the source, the compilation with EEx happens in `eex_engine` here  https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/template/eex_engine.ex#L8,
and the generate functions for each template are defined here https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/template.ex#L373-L393.

It doesn't seem to call `EEx.function_from_file`, but the end result is pretty much the same.
Reply all
Reply to author
Forward
0 new messages