#development #elixir #phoenix

Templates in a Phoenix application ultimately get compiled to functions that can be quickly rendered with the necessary data. We can take a look at how a template will be rendered using Phoenix.View.render_to_string/3.

First, we need a template:

1# user.html.eex
2<h1><%= @user.first_name %></h1>
3<h5><%= @user.username %> (<%= @user.email %>)</h5>

We can then render that template for the view with some user:

1> user = %User{first_name: "Liz", last_name: "Lemon", username: "llemon", email: "lizlemon@nbc.com"}
2%MyApp.User{...}
3
4> Phoenix.View.Render_to_string(MyApp.UserView, "user.html", user: user)
5"<h1>Liz</h1>\n<h5>llemon (lizlemon@nbc.com)</h5>\n"