<%= error_to_string(err) %>
- <% end %> - +{error_to_string(err)}
<%= error_to_string(err) %>
-<% end %> + <%!-- Phoenix.Component.upload_errors/1 returns a list of error atoms --%> ++ {error_to_string(err)} +
diff --git a/.github/single-file-samples/main.exs b/.github/single-file-samples/main.exs index 88b63afcee..7f2171bfd1 100644 --- a/.github/single-file-samples/main.exs +++ b/.github/single-file-samples/main.exs @@ -43,13 +43,13 @@ defmodule Example.HomeLive do - <%= @inner_content %> + {@inner_content} """ end def render(assigns) do ~H""" - <%= @count %> + {@count} """ diff --git a/.github/single-file-samples/test.exs b/.github/single-file-samples/test.exs index d494dbd173..b78bb6a8d6 100644 --- a/.github/single-file-samples/test.exs +++ b/.github/single-file-samples/test.exs @@ -39,7 +39,7 @@ defmodule Example.HomeLive do - <%= @inner_content %> + {@inner_content} """ end diff --git a/guides/client/bindings.md b/guides/client/bindings.md index b2987b2ffe..951a947348 100644 --- a/guides/client/bindings.md +++ b/guides/client/bindings.md @@ -148,7 +148,7 @@ for example: def render(assigns) do ~H"""
- <%= Phoenix.Flash.get(@flash, :info) %> + {Phoenix.Flash.get(@flash, :info)}
``` diff --git a/guides/client/js-interop.md b/guides/client/js-interop.md index 15d416819c..801dcf8eb9 100644 --- a/guides/client/js-interop.md +++ b/guides/client/js-interop.md @@ -78,7 +78,7 @@ element from the server to draw the user's attention: ```heexHello <%= @name %>
+Hello {@name}
<%= error_to_string(err) %>
- <% end %> - +{error_to_string(err)}
<%= error_to_string(err) %>
-<% end %> + <%!-- Phoenix.Component.upload_errors/1 returns a list of error atoms --%> ++ {error_to_string(err)} +
Hello, <%= @name %>!
+Hello, {@name}!
""" end end @@ -20,8 +20,9 @@ defmodule Phoenix.Component do This function uses the `~H` sigil to return a rendered template. `~H` stands for HEEx (HTML + EEx). HEEx is a template language for writing HTML mixed with Elixir interpolation. We can write Elixir - code inside HEEx using `<%= ... %>` tags and we use `@name` to - access the key `name` defined inside `assigns`. + code inside `{...}` for HTML-aware interpolation inside tag attributes + and the body. We can also interpolate arbitrary HEEx blocks using `<%= ... %>` + We use `@name` to access the key `name` defined inside `assigns`. When invoked within a `~H` sigil or HEEx template file: @@ -68,7 +69,7 @@ defmodule Phoenix.Component do def greet(assigns) do ~H""" -Hello, <%= @name %>!
+Hello, {@name}!
""" end @@ -110,8 +111,8 @@ defmodule Phoenix.Component do def celebrate(assigns) do ~H"""- Happy birthday <%= @name %>! - You are <%= @age %> years old. + Happy birthday {@name}! + You are {@age} years old.
""" end @@ -143,7 +144,7 @@ defmodule Phoenix.Component do def heading(assigns) do ~H""" -Hello <%= @name %>
+Hello {@name}
""" end end @@ -178,7 +179,7 @@ defmodule Phoenix.Component do def notification(assigns) do ~H""" - <%= @message %> + {@message} """ end @@ -233,7 +234,7 @@ defmodule Phoenix.Component do slot :inner_block def button(assigns) do ~H""" - + """ end ``` @@ -281,7 +282,7 @@ defmodule Phoenix.Component do def button(assigns) do ~H""" """ end @@ -327,10 +328,8 @@ defmodule Phoenix.Component do def unordered_list(assigns) do ~H""" -<%= col.label %> | - <% end %> +
---|
{col.label} | +
{render_slot(col, row)} |
<%= render_slot(col, row) %> | - <% end %> -
Hello <%= @name %>
+Hello {@name}
Hello, <%= @name %>
+Hello, {@name}
``` - Similarly, conditionals and other block Elixir constructs are supported: - - ```heex - <%= if @show_greeting? do %> -Hello, <%= @name %>
- <% end %> - ``` - - Note we don't include the equal sign `=` in the closing `<% end %>` tag - (because the closing tag does not output anything). - - There is one important difference between `HEEx` and Elixir's builtin `EEx`. - `HEEx` uses a specific annotation for interpolating HTML tags and attributes. - Let's check it out. - - ### HEEx extension: Defining attributes - - Since `HEEx` must parse and validate the HTML structure, code interpolation using - `<%= ... %>` and `<% ... %>` are restricted to the body (inner content) of the - HTML/component nodes and it cannot be applied within tags. - - For instance, the following syntax is invalid: - - ```heex -Hello, {@name}
+ <% end %> + ``` - It is typically best to group related functions into a single module, as - opposed to having many modules with a single `render/1` function. Function - components support other important features, such as slots. You can learn - more about components in `Phoenix.Component`. + However, for conditionals and for-comprehensions, there are built-in constructs + in HEEx too, which we will explore next. - ### HEEx extension: special attributes + ### Special attributes Apart from normal HTML attributes, HEEx also supports some special attributes such as `:let` and `:for`. @@ -742,7 +694,7 @@ defmodule Phoenix.Component do ```heex<%= user.name %> | +{user.name} |
<%= user[header] %> | +{user[header]} |
<%= col.label %> | - <% end %> +
---|
{col.label} | +
{render_slot(col, row)} |
<%= render_slot(col, row) %> | - <% end %> -
<%= live_flash(@flash, :info) %>
-<%= live_flash(@flash, :error) %>
+{live_flash(@flash, :info)}
+{live_flash(@flash, :error)}
``` """ @deprecated "Use Phoenix.Flash.get/2 in Phoenix v1.7+" @@ -1071,7 +1059,7 @@ defmodule Phoenix.Component do ```heex<%= error %>
+{error}
<%= error %>
+{error}
- Happy birthday <%= @name %>! - You are <%= @age %> years old. + Happy birthday {@name}! + You are {@age} years old.
""" end @@ -2089,13 +2077,13 @@ defmodule Phoenix.Component do ```heex <.live_title default="Welcome" prefix="MyApp – "> - <%= assigns[:page_title] %> + {assigns[:page_title]} ``` ```heex <.live_title default="Welcome" suffix="- MyApp"> - <%= assigns[:page_title] %> + {assigns[:page_title]} ``` """ @@ -2117,7 +2105,7 @@ defmodule Phoenix.Component do def live_title(assigns) do ~H""" -- <%= Phoenix.Flash.get(@flash, :info) %> + {Phoenix.Flash.get(@flash, :info)}
``` """ @@ -730,7 +730,7 @@ defmodule Phoenix.LiveView do ```heex- <%= Phoenix.Flash.get(@flash, :info) %> + {Phoenix.Flash.get(@flash, :info)}
``` """ @@ -1264,11 +1264,9 @@ defmodule Phoenix.LiveView do And then in your views: ```heex - <%= if @static_changed? do %> -, - <%= @inner_content %> + {@inner_content} """ end @@ -138,7 +138,7 @@ defmodule Phoenix.LiveViewTest.E2E.ErrorLive do @impl Phoenix.LiveView def render(assigns) do ~H""" -main rendered at: <%= DateTime.utc_now() %>
+main rendered at: {DateTime.utc_now()}
@@ -150,7 +150,7 @@ defmodule Phoenix.LiveViewTest.E2E.ErrorLive do<%= if assigns[:child] do %> - <%= live_render(@socket, ChildLive, id: "child") %> + {live_render(@socket, ChildLive, id: "child")} <% end %>diff --git a/test/e2e/support/form_feedback.ex b/test/e2e/support/form_feedback.ex index 5755b71e45..0f6c2cdcc0 100644 --- a/test/e2e/support/form_feedback.ex +++ b/test/e2e/support/form_feedback.ex @@ -73,7 +73,7 @@ defmodule Phoenix.LiveViewTest.E2E.FormFeedbackLive do window.liveSocket = liveSocket - <%= @inner_content %> + {@inner_content} """ end @@ -111,15 +111,15 @@ defmodule Phoenix.LiveViewTest.E2E.FormFeedbackLive do display: none; } -Button Count: <%= @count %>
-Validate Count: <%= @validate_count %>
-Submit Count: <%= @submit_count %>
+Button Count: {@count}
+Validate Count: {@validate_count}
+Submit Count: {@submit_count}
<.myform /> - <% # render inside function component to trigger the phx-magic-id optimization %> + {# render inside function component to trigger the phx-magic-id optimization} <.myfeedback feedback={@feedback} /> diff --git a/test/e2e/support/form_live.ex b/test/e2e/support/form_live.ex index ff214c2512..f8303644cb 100644 --- a/test/e2e/support/form_live.ex +++ b/test/e2e/support/form_live.ex @@ -156,11 +156,11 @@ defmodule Phoenix.LiveViewTest.E2E.NestedFormLive do def render(assigns) do ~H""" - <%= live_render(@socket, Phoenix.LiveViewTest.E2E.FormLive, + {live_render(@socket, Phoenix.LiveViewTest.E2E.FormLive, id: "nested", layout: nil, session: @params - ) %> + )} """ end end @@ -170,14 +170,14 @@ defmodule Phoenix.LiveViewTest.E2E.FormStreamLive do def render(assigns) do ~H""" - <%= @count %> + {@count}
<%= error_to_string(err) %>
+{error_to_string(err)}
<% end %> diff --git a/test/e2e/support/issues/issue_3026.ex b/test/e2e/support/issues/issue_3026.ex index 271b656fd0..855ce7a827 100644 --- a/test/e2e/support/issues/issue_3026.ex +++ b/test/e2e/support/issues/issue_3026.ex @@ -57,7 +57,7 @@ defmodule Phoenix.LiveViewTest.E2E.Issue3026Live do ~H""" <.form for={to_form(%{})} phx-change="change_status"> @@ -77,7 +77,7 @@ defmodule Phoenix.LiveViewTest.E2E.Issue3026Live do defp status(assigns) do ~H"""Current param: <%= @param_current %>
+Current param: {@param_current}
<.styled_link patch={"/navigation/a?param=#{@param_next}"}>Patch this LiveView <.styled_link patch={"/navigation/a?param=#{@param_next}"} replace>Patch (Replace) @@ -88,7 +88,7 @@ defmodule Phoenix.LiveViewTest.E2E.Navigation.ALive do style="padding-left: 1rem; padding-right: 1rem; padding-top: 0.5rem; padding-bottom: 0.5rem; background-color: #e2e8f0; display: inline-flex; align-items: center; border-radius: 0.375rem; cursor: pointer;" {Map.delete(assigns, [:inner_block])} > - <%= render_slot(@inner_block) %> + {render_slot(@inner_block)} """ end @@ -148,7 +148,7 @@ defmodule Phoenix.LiveViewTest.E2E.Navigation.BLive do patch={"/navigation/b/#{item.id}"} style="display: inline-flex; align-items: center; gap: 0.5rem;" > - Item <%= item.name %> + Item {item.name} <% end %> @@ -156,7 +156,7 @@ defmodule Phoenix.LiveViewTest.E2E.Navigation.BLive doItem <%= @id %>
+Item {@id}
- <%= error_to_string(err) %> + {error_to_string(err)}
""" diff --git a/test/e2e/test_helper.exs b/test/e2e/test_helper.exs index b99669a6c7..f599861699 100644 --- a/test/e2e/test_helper.exs +++ b/test/e2e/test_helper.exs @@ -25,7 +25,7 @@ defmodule Phoenix.LiveViewTest.E2E.Layout do def render("root.html", assigns) do ~H""" - <%!-- no doctype -> quirks mode --%> <%= @inner_content %> + <%!-- no doctype -> quirks mode --%> {@inner_content} """ end @@ -56,7 +56,7 @@ defmodule Phoenix.LiveViewTest.E2E.Layout do - <%= @inner_content %> + {@inner_content} """ end end diff --git a/test/phoenix_component/components_test.exs b/test/phoenix_component/components_test.exs index c076f12178..15253dd351 100644 --- a/test/phoenix_component/components_test.exs +++ b/test/phoenix_component/components_test.exs @@ -154,7 +154,7 @@ defmodule Phoenix.LiveView.ComponentsTest do test "dynamic attrs" do assigns = %{prefix: "MyApp – ", title: "My Title"} - assert t2h(~H|<.live_title prefix={@prefix}><%= @title %>|) == + assert t2h(~H|<.live_title prefix={@prefix}>{@title}|) == ~X|<%= finner.options[:foo] %>
+{finner.options[:foo]}
""" @@ -694,7 +694,7 @@ defmodule Phoenix.LiveView.ComponentsTest do template = ~H""" <.intersperse :let={item} enum={[1, 2, 3]}> <:separator>| - Item<%= item %> + Item{item} """ @@ -704,7 +704,7 @@ defmodule Phoenix.LiveView.ComponentsTest do template = ~H""" <.intersperse :let={item} enum={[1]}> <:separator>| - Item<%= item %> + Item{item} """ diff --git a/test/phoenix_component/declarative_assigns_test.exs b/test/phoenix_component/declarative_assigns_test.exs index b90daa17e8..3912d7b328 100644 --- a/test/phoenix_component/declarative_assigns_test.exs +++ b/test/phoenix_component/declarative_assigns_test.exs @@ -71,19 +71,19 @@ defmodule Phoenix.ComponentDeclarativeAssignsTest do def button_with_values_line, do: __ENV__.line attr :text, :string, values: ["Save", "Cancel"] - def button_with_values(assigns), do: ~H[] + def button_with_values(assigns), do: ~H[] def button_with_values_and_default_1_line, do: __ENV__.line attr :text, :string, values: ["Save", "Cancel"], default: "Save" - def button_with_values_and_default_1(assigns), do: ~H[] + def button_with_values_and_default_1(assigns), do: ~H[] def button_with_values_and_default_2_line, do: __ENV__.line attr :text, :string, default: "Save", values: ["Save", "Cancel"] - def button_with_values_and_default_2(assigns), do: ~H[] + def button_with_values_and_default_2(assigns), do: ~H[] def button_with_examples_line, do: __ENV__.line attr :text, :string, examples: ["Save", "Cancel"] - def button_with_examples(assigns), do: ~H[] + def button_with_examples(assigns), do: ~H[] def render_line, do: __ENV__.line @@ -385,13 +385,13 @@ defmodule Phoenix.ComponentDeclarativeAssignsTest do<%= col.label %> | +{col.label} | <% end %>
---|---|
<%= render_slot(col, row) %> | +{render_slot(col, row)} | <% end %>