From 1093a4d60261dec00a90fb27239df282d343c701 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Wed, 8 Apr 2020 22:23:27 -0400 Subject: [PATCH] Rename `props` to `assigns` This helps stay consistent with the Phoenix nomenclature. --- README.md | 2 +- lib/temple.ex | 14 +++++++------- lib/temple/utils.ex | 16 ++++++++-------- test/support/component.ex | 4 ++-- test/temple_test.exs | 36 ++++++++++++++++++------------------ 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 61e1218..f3ec50f 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ end Temple provides an API for creating custom components that act as custom HTML elements. -These components can be given `props` that are available inside the component definition as module attributes. The contents of a components `do` block are available as a special `@children` attribute. +These components can be given `assigns` that are available inside the component definition as module attributes. The contents of a components `do` block are available as a special `@children` assign. See the [documentation](https://hexdocs.pm/temple/Temple.html#defcomponent/2) for more details. diff --git a/lib/temple.ex b/lib/temple.ex index ed69d0c..5899061 100644 --- a/lib/temple.ex +++ b/lib/temple.ex @@ -114,7 +114,7 @@ defmodule Temple do Components accept a keyword list or a map of assigns and can be referenced in the body of the component by a module attribute of the same name. - This works exactly the same as EEx templates. The whole list or map of assigns can be accessed by the special `@props` assign. + This works exactly the same as EEx templates. The whole list or map of assigns can be accessed by a special assign called `@assigns`. ## Children @@ -152,28 +152,28 @@ defmodule Temple do Temple.Utils.__quote__(outer) end - defmacro unquote(name)(props_or_block) + defmacro unquote(name)(assigns_or_block) defmacro unquote(name)([{:do, inner}]) do outer = unquote(Macro.escape(block)) - |> Temple.Utils.__insert_props__([], inner) + |> Temple.Utils.__insert_assigns__([], inner) Temple.Utils.__quote__(outer) end - defmacro unquote(name)(props) do + defmacro unquote(name)(assigns) do outer = unquote(Macro.escape(block)) - |> Temple.Utils.__insert_props__(props, nil) + |> Temple.Utils.__insert_assigns__(assigns, nil) Temple.Utils.__quote__(outer) end - defmacro unquote(name)(props, inner) do + defmacro unquote(name)(assigns, inner) do outer = unquote(Macro.escape(block)) - |> Temple.Utils.__insert_props__(props, inner) + |> Temple.Utils.__insert_assigns__(assigns, inner) Temple.Utils.__quote__(outer) end diff --git a/lib/temple/utils.ex b/lib/temple/utils.ex index 5ac6a75..4e4f631 100644 --- a/lib/temple/utils.ex +++ b/lib/temple/utils.ex @@ -35,21 +35,21 @@ defmodule Temple.Utils do partial |> Phoenix.HTML.html_escape() |> Phoenix.HTML.safe_to_string() end - def insert_props({:@, _, [{:children, _, _}]}, _, inner) do + def insert_assigns({:@, _, [{:children, _, _}]}, _, inner) do inner end - def insert_props({:@, _, [{:props, _, _}]}, props, _) do - props + def insert_assigns({:@, _, [{:assigns, _, _}]}, assigns, _) do + assigns end - def insert_props({:@, _, [{name, _, _}]}, props, _) when is_atom(name) do + def insert_assigns({:@, _, [{name, _, _}]}, assigns, _) when is_atom(name) do quote location: :keep do - Access.get(unquote_splicing([props, name])) + Access.get(unquote_splicing([assigns, name])) end end - def insert_props(ast, _, _), do: ast + def insert_assigns(ast, _, _), do: ast def compile_attrs([]), do: "" @@ -87,9 +87,9 @@ defmodule Temple.Utils do quote [location: :keep], do: unquote(outer) end - def __insert_props__(block, props, inner) do + def __insert_assigns__(block, assigns, inner) do block - |> Macro.prewalk(&Temple.Utils.insert_props(&1, props, inner)) + |> Macro.prewalk(&Temple.Utils.insert_assigns(&1, assigns, inner)) end def doc_path(:html, el), do: "./tmp/docs/html/#{el}.txt" diff --git a/test/support/component.ex b/test/support/component.ex index dcaac40..a33887a 100644 --- a/test/support/component.ex +++ b/test/support/component.ex @@ -13,8 +13,8 @@ defmodule Component do end end - defcomponent :lists_props do - partial inspect(@props) |> Phoenix.HTML.raw() + defcomponent :lists_assigns do + partial inspect(@assigns) |> Phoenix.HTML.raw() end defcomponent :arbitrary_code do diff --git a/test/temple_test.exs b/test/temple_test.exs index e546404..699dfe4 100644 --- a/test/temple_test.exs +++ b/test/temple_test.exs @@ -76,30 +76,30 @@ defmodule TempleTest do ~s{
mitch
} end - test "can access props list" do + test "can access assigns list" do import Component - props = [foo: "bar", hello: "world"] + assigns = [foo: "bar", hello: "world"] {:safe, result} = temple do - lists_props(props) + lists_assigns(assigns) end - assert result == inspect(props) + assert result == inspect(assigns) end - test "can access props map" do + test "can access assigns map" do import Component - props = %{foo: "bar", hello: "world"} + assigns = %{foo: "bar", hello: "world"} {:safe, result} = temple do - lists_props(props) + lists_assigns(assigns) end - assert result == inspect(props) + assert result == inspect(assigns) end test "can have arbitrary code inside the definition" do @@ -125,7 +125,7 @@ defmodule TempleTest do assert result == ~s{
} end - test "can pass arbitrary data as props" do + test "can pass arbitrary data as assigns" do import Component {:safe, result} = @@ -167,27 +167,27 @@ defmodule TempleTest do assert result == ~s|
| end - test "can pass all of the props as a variable" do + test "can pass all of the assigns as a variable" do import Component - props = [bob: "hi"] + assigns = [bob: "hi"] {:safe, result} = temple do - variable_as_prop(props) + variable_as_prop(assigns) end assert result == ~s|
| end - test "can pass all of the props as a variable with a block" do + test "can pass all of the assigns as a variable with a block" do import Component - props = [bob: "hi"] + assigns = [bob: "hi"] {:safe, result} = temple do - variable_as_prop_with_block props do + variable_as_prop_with_block assigns do div() end end @@ -195,14 +195,14 @@ defmodule TempleTest do assert result == ~s|
| end - test "can pass a map as props with a block" do + test "can pass a map as assigns with a block" do import Component - props = %{bob: "hi"} + assigns = %{bob: "hi"} {:safe, result} = temple do - variable_as_prop_with_block props do + variable_as_prop_with_block assigns do div() end