You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/components.md
+18-16Lines changed: 18 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ At the end of the Request life-cycle chapter, we created a template at `lib/hell
16
16
17
17
```heex
18
18
<section>
19
-
<h2>Hello World, from <%= @messenger %>!</h2>
19
+
<h2>Hello World, from {@messenger}!</h2>
20
20
</section>
21
21
```
22
22
@@ -34,7 +34,7 @@ That's simple enough. There's only two lines, `use HelloWeb, :html`. This line c
34
34
35
35
All of the imports and aliases we make in our module will also be available in our templates. That's because templates are effectively compiled into functions inside their respective module. For example, if you define a function in your module, you will be able to invoke it directly from the template. Let's see this in practice.
36
36
37
-
Imagine we want to refactor our `show.html.heex` to move the rendering of `<h2>Hello World, from <%= @messenger %>!</h2>` to its own function. We can move it to a function component inside `HelloHTML`, let's do so:
37
+
Imagine we want to refactor our `show.html.heex` to move the rendering of `<h2>Hello World, from {@messenger}!</h2>` to its own function. We can move it to a function component inside `HelloHTML`, let's do so:
38
38
39
39
```elixir
40
40
defmoduleHelloWeb.HelloHTMLdo
@@ -46,7 +46,7 @@ defmodule HelloWeb.HelloHTML do
46
46
47
47
defgreet(assigns) do
48
48
~H"""
49
-
<h2>Hello World, from <%=@messenger%>!</h2>
49
+
<h2>Hello World, from {@messenger}!</h2>
50
50
"""
51
51
end
52
52
end
@@ -89,19 +89,21 @@ Next, let's fully understand the expressive power behind the HEEx template langu
89
89
90
90
## HEEx
91
91
92
-
Function components and templates files are powered by [the HEEx template language](https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#sigil_H/2), which stands for "HTML+EEx". EEx is an Elixir library that uses `<%= expression %>` to execute Elixir expressions and interpolate their results into the template. This is frequently used to display assigns we have set by way of the `@` shortcut. In your controller, if you invoke:
92
+
Function components and templates files are powered by [the HEEx template language](https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#sigil_H/2), which stands for "HTML+EEx". EEx is an Elixir library that uses `<%= expression %>` to execute Elixir expressions and interpolate their results into arbitrary text templates. HEEx extends EEx for writing HTML templates mixed with Elixir interpolation. We can write Elixir code inside `{...}` for HTML-aware interpolation inside tag attributes and the body. We can also interpolate arbitrary HEEx blocks using EEx interpolation (`<%= ... %>`). We use `@name` to access the key `name` defined inside `assigns`.
93
+
94
+
This is frequently used to display assigns we have set by way of the `@` shortcut. In your controller, if you invoke:
93
95
94
96
```elixir
95
97
render(conn, :show, username:"joe")
96
98
```
97
99
98
-
Then you can access said username in the templates as `<%= @username %>`. In addition to displaying assigns and functions, we can use pretty much any Elixir expression. For example, in order to have conditionals:
100
+
Then you can access said username in the templates as `{@username}`. In addition to displaying assigns and functions, we can use pretty much any Elixir expression. For example, in order to have conditionals:
99
101
100
102
```heex
101
103
<%= if some_condition? do %>
102
-
<p>Some condition is true for user: <%= @username %></p>
104
+
<p>Some condition is true for user: {@username}</p>
103
105
<% else %>
104
-
<p>Some condition is false for user: <%= @username %></p>
106
+
<p>Some condition is false for user: {@username}</p>
105
107
<% end %>
106
108
```
107
109
@@ -115,8 +117,8 @@ or even loops:
115
117
</tr>
116
118
<%= for number <- 1..10 do %>
117
119
<tr>
118
-
<td><%= number %></td>
119
-
<td><%= number * number %></td>
120
+
<td>{number}</td>
121
+
<td>{number * number}</td>
120
122
</tr>
121
123
<% end %>
122
124
</table>
@@ -131,20 +133,20 @@ HEEx also comes with handy HTML extensions we will learn next.
131
133
Besides allowing interpolation of Elixir expressions via `<%= %>`, `.heex` templates come with HTML-aware extensions. For example, let's see what happens if you try to interpolate a value with "<" or ">" in it, which would lead to HTML injection:
132
134
133
135
```heex
134
-
<%= "<b>Bold?</b>" %>
136
+
{"<b>Bold?</b>"}
135
137
```
136
138
137
139
Once you render the template, you will see the literal `<b>` on the page. This means users cannot inject HTML content on the page. If you want to allow them to do so, you can call `raw`, but do so with extreme care:
138
140
139
141
```heex
140
-
<%= raw"<b>Bold?</b>" %>
142
+
{raw("<b>Bold?</b>")}
141
143
```
142
144
143
-
Another super power of HEEx templates is validation of HTML and lean interpolation syntax of attributes. You can write:
145
+
Another super power of HEEx templates is validation of HTML and interpolation syntax of attributes. You can write:
144
146
145
147
```heex
146
148
<div title="My div" class={@class}>
147
-
<p>Hello <%= @username %></p>
149
+
<p>Hello {@username}</p>
148
150
</div>
149
151
```
150
152
@@ -154,7 +156,7 @@ To interpolate a dynamic number of attributes in a keyword list or map, do:
154
156
155
157
```heex
156
158
<div title="My div" {@many_attributes}>
157
-
<p>Hello <%= @username %></p>
159
+
<p>Hello {@username}</p>
158
160
</div>
159
161
```
160
162
@@ -178,7 +180,7 @@ Likewise, for comprehensions may be written as:
178
180
179
181
```heex
180
182
<ul>
181
-
<li :for={item <- @items}><%= item.name %></li>
183
+
<li :for={item <- @items}>{item.name}</li>
182
184
</ul>
183
185
```
184
186
@@ -189,7 +191,7 @@ Layouts are just function components. They are defined in a module, just like al
189
191
You may be wondering how the string resulting from a rendered view ends up inside a layout. That's a great question! If we look at `lib/hello_web/components/layouts/root.html.heex`, just about at the end of the `<body>`, we will see this.
190
192
191
193
```heex
192
-
<%= @inner_content %>
194
+
{@inner_content}
193
195
```
194
196
195
197
In other words, after rendering your page, the result is placed in the `@inner_content` assign.
Copy file name to clipboardExpand all lines: guides/plug.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,7 +120,7 @@ In the [`init/1`] callback, we pass a default locale to use if none is present i
120
120
To see the assign in action, go to the template in `lib/hello_web/controllers/page_html/home.html.heex` and add the following code after the closing of the `</h1>` tag:
121
121
122
122
```heex
123
-
<p>Locale: <%= @locale %></p>
123
+
<p>Locale: {@locale}</p>
124
124
```
125
125
126
126
Go to [http://localhost:4000/](http://localhost:4000/) and you should see the locale exhibited. Visit [http://localhost:4000/?locale=fr](http://localhost:4000/?locale=fr) and you should see the assign changed to `"fr"`. Someone can use this information alongside [Gettext](https://hexdocs.pm/gettext/Gettext.html) to provide a fully internationalized web application.
Copy file name to clipboardExpand all lines: guides/request_lifecycle.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,7 @@ Now that we've got the route, controller, view, and template, we should be able
181
181
There are a couple of interesting things to notice about what we just did. We didn't need to stop and restart the server while we made these changes. Yes, Phoenix has hot code reloading! Also, even though our `index.html.heex` file consists of only a single `section` tag, the page we get is a full HTML document. Our index template is actually rendered into layouts: first it renders `lib/hello_web/components/layouts/root.html.heex` which renders `lib/hello_web/components/layouts/app.html.heex` which finally includes our content. If you open those files, you'll see a line that looks like this at the bottom:
182
182
183
183
```heex
184
-
<%= @inner_content %>
184
+
{@inner_content}
185
185
```
186
186
187
187
This line injects our template into the layout before the HTML is sent off to the browser. We will talk more about layouts in the Controllers guide.
@@ -273,15 +273,17 @@ It's good to remember that the keys of the `params` map will always be strings,
273
273
274
274
For the last piece of this puzzle, we'll need a new template. Since it is for the `show` action of `HelloController`, it will go into the `lib/hello_web/controllers/hello_html` directory and be called `show.html.heex`. It will look surprisingly like our `index.html.heex` template, except that we will need to display the name of our messenger.
275
275
276
-
To do that, we'll use the special HEEx tags for executing Elixir expressions: `<%= %>`. Notice that the initial tag has an equals sign like this: `<%=` . That means that any Elixir code that goes between those tags will be executed, and the resulting value will replace the tag in the HTML output. If the equals sign were missing, the code would still be executed, but the value would not appear on the page.
276
+
To do that, we'll use the special HEEx tags for executing Elixir expressions: `{...}` and `<%= %>`. Notice that EEx tag has an equals sign like this: `<%=` . That means that any Elixir code that goes between those tags will be executed, and the resulting value will replace the tag in the HTML output. If the equals sign were missing, the code would still be executed, but the value would not appear on the page.
277
277
278
-
Remember our templates are written in HEEx (HTML+EEx). HEEx is a superset of EEx which is why it shares the `<%= %>` syntax.
278
+
Remember our templates are written in HEEx (HTML+EEx). HEEx is a superset of EEx, and thereby supports the EEx `<%= %>`interpolation syntax for interpolating arbitrary blocks of code. In general, the HEEx `{...}` interpolation syntax is preferred anytime there is HTML-aware intepolation to be done – such as within attributes or inline values with a body.
279
279
280
-
And this is what the template should look like:
280
+
The only times `EEx``<%= %>` interpolation is necessary is for interpolationg arbitrary blocks of markup, such as branching logic that inects separate markup trees, or for interpolating values within `<script>` or `<style>` tags.
281
+
282
+
This is what the `hello_html/show.html.heex` template should look like:
0 commit comments