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
WARNING: This document may not be up to date with the latest Phoenix/LiveView
6
-
version. I am working on an updated version.
5
+
WARNING: I am currently in the process of updating this document to the latest Elixir and Phoenix version. Please help me by sending me bug reports (ideally as GitHub issues). Thank you!
@@ -63,10 +62,9 @@ You can also run your app inside IEx (Interactive Elixir) as:
63
62
64
63
$ iex -S mix phx.server
65
64
----
66
-
<1> '--live' adds all the needed stuff to use LiveView out of the box. For this
67
-
example, we don't need Ecto.
68
-
<2> Click `Y` and, depending on your internet connection, maybe grab a cup of
69
-
coffee.
65
+
<1> The `--live` flag adds all the necessary components to use LiveView out of the box. For this
66
+
example, we don't need Ecto, so we use `--no-ecto`. Phoenix 1.7+ comes with Tailwind CSS by default.
67
+
<2> Click `Y` and, depending on your internet connection, maybe grab a cup of coffee.
70
68
71
69
The aim of this demo is to create a new webpage with the path `/light` which
72
70
offers a status of a virtual light bulb and a switch functionality to turn that
@@ -75,7 +73,7 @@ any JavaScript.
75
73
76
74
First we have to add the route:
77
75
78
-
.lib/hello_world_web/router.ex
76
+
.lib/demo_web/router.ex
79
77
[source,elixir]
80
78
----
81
79
defmodule DemoWeb.Router do
@@ -86,15 +84,13 @@ defmodule DemoWeb.Router do
86
84
scope "/", DemoWeb do
87
85
pipe_through :browser
88
86
89
-
live "/", PageLive, :index <1>
87
+
get "/", PageController, :home <1>
90
88
live "/light", LightLive <2>
91
89
end
92
90
93
91
[...]
94
92
----
95
-
<1> Because we created the app with the `--live` switch the default root path is
96
-
already a live view. Therefore the `live` macro is used here (at the beginning
97
-
of the line) instead of the traditional `get`.
93
+
<1> In Phoenix 1.7+, the default root path uses a regular controller instead of a LiveView. You can change this to a LiveView if desired.
98
94
<2> This is the new `light` route which leads to the `LightLive` module.
99
95
100
96
LiveView modules are located in the `lib/demo_web/live/` directory. There we
@@ -107,27 +103,26 @@ defmodule DemoWeb.LightLive do
107
103
use DemoWeb, :live_view
108
104
109
105
def render(assigns) do <1>
110
-
~L"""
111
-
<h1>The light is off.</h1>
106
+
~H"""
107
+
<h1 class="text-2xl font-bold mb-4">The light is off.</h1>
112
108
"""
113
109
end
114
110
end
115
111
----
116
-
<1> The `render/1` function renders the template. We use the `~L` sigil to
117
-
define the template.
112
+
<1> The `render/1` function renders the template. In Phoenix 1.7+, we use the `~H` sigil for HEEx templates instead of the deprecated `~L` sigil. Note the Tailwind CSS classes added to the h1 element.
118
113
119
114
[IMPORTANT]
120
115
====
121
-
In this case, we use the `~L` sigil to define the template, but for bigger
116
+
In this case, we use the `~H` sigil to define the template inline, but for bigger
122
117
templates, it makes more sense to create a separate template file, which would
123
-
be a LiveEEx template (using the `.leex` extension) and be stored in the
118
+
be a HEEx template (using the `.heex` extension) and be stored in the
124
119
`lib/demo_web/live` directory. If you use a separate template file, the
125
120
`render/1` function is not needed (see the Airport Code Search section for an
126
121
example).
127
122
128
123
Have a look at https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html for more information.
129
124
====
130
-
indexterm:["LiveEEx Templates"]
125
+
indexterm:["HEEx Templates"]
131
126
132
127
Now, let's open the URL `http://localhost:4000/light` in the browser.
133
128
@@ -152,13 +147,13 @@ defmodule DemoWeb.LightLive do
152
147
end
153
148
154
149
def render(assigns) do
155
-
~L"""
156
-
<h1>The light is <%= @light_bulb_status %>.</h1>
150
+
~H"""
151
+
<h1 class="text-2xl font-bold mb-4">The light is <%= @light_bulb_status %>.</h1>
157
152
"""
158
153
end
159
154
end
160
155
----
161
-
<1> Out of all the posssible parameters of `mount/3` we only need the `socket`
156
+
<1> Out of all the possible parameters of `mount/3` we only need the `socket`
162
157
struct for our example.
163
158
<2> We set the initial value of the variable `light_bulb_status` to `off`.
164
159
@@ -170,14 +165,19 @@ To turn on the light bulb we need a button:
170
165
[source,elixir]
171
166
----
172
167
def render(assigns) do
173
-
~L"""
174
-
<h1>The light is <%= @light_bulb_status %>.</h1>
175
-
<button phx-click="on">On</button> <1>
168
+
~H"""
169
+
<h1 class="text-2xl font-bold mb-4">The light is <%= @light_bulb_status %>.</h1>
<1> Setting the first input field to `autofocus` improves user experience. The `autocomplete="off"` prevents browser autocomplete from interfering with our LiveView updates.
653
+
<2> When the search returns a non-empty list, a table with the results will be displayed, styled with Tailwind CSS.
647
654
648
655
Lastly, we need to update the TravelagentWeb.SearchLive module:
649
656
@@ -700,40 +707,48 @@ codes begin with an `h`. Without having to click on the `Search Airport` button.
700
707
Luckily for us, we only have to make a couple of changes in the LiveEEx Template
0 commit comments