Once you render the template, you will see the literal on the page. For example, let's see what happens if you try to interpolate a value with "" in it, which would lead to HTML injection: Bold?" %> heex templates come with HTML-aware extensions.
If this is not included the code will still be executed but nothing will be inserted into the template.īesides allowing interpolation of Elixir expressions via. Or even loops: Number Power ĭid you notice the use of versus above? All expressions that output something to the template must use the equals sign ( =). For example, in order to have conditionals: Some condition is true for user: Some condition is false for user: In addition to displaying assigns and functions, we can use pretty much any Elixir expression. Then you can access said username in the templates as. In your controller, if you invoke: render ( conn, "show.html", username : "joe" ) This is frequently used to display assigns we have set by way of the shortcut. EEx is an Elixir library that uses to execute Elixir expressions and interpolate their results into the template. Since templates are compiled inside the view, we could invoke the view function simply as title(), otherwise we would have to type ().Īs you may recall, Elixir templates use. When we reload our home page, we should see our new title. defmodule HelloWeb.LayoutView do use HelloWeb, :view def title ( ) do "Awesome New Title!" end end Now let's add a title/0 function to our LayoutView. Open up our application layout template, lib/hello_web/templates/layout/, and change this line,
For example, if you define a function in your view, you will be able to invoke it directly from the template. That's because templates are effectively compiled into functions inside their respective views. This line calls the view/0 function defined in HelloWeb which sets up the basic imports and configuration for our views and templates.Īll of the imports and aliases we make in our view will also be available in our templates. There's only one line, use HelloWeb, :view. defmodule HelloWeb.LayoutView do use HelloWeb, :view end While all of these can be customizable (see Phoenix.View and Phoenix.Template for more information), we recommend users stick with Phoenix' convention.Ī newly generated Phoenix application has three view modules - ErrorView, LayoutView, and PageView - which are all in the lib/hello_web/views/ directory. PageController requires a PageView to render templates in the lib/hello_web/templates/page/ directory. Phoenix assumes a strong naming convention from controllers to views to the templates they render. Most of the time, we use templates to build these responses, but we can also craft them by hand. The main job of a Phoenix view is to render the body of the response which gets sent back to browsers and to API clients. Requirement: This guide expects that you have gone through the request life-cycle guide. Requirement: This guide expects that you have gone through the introductory guides and got a Phoenix application up and running.