Rename props to assigns

This helps stay consistent with the Phoenix nomenclature.
This commit is contained in:
Mitchell Hanberg 2020-04-08 22:23:27 -04:00
parent fa41e73bb0
commit 1093a4d602
5 changed files with 36 additions and 36 deletions

View file

@ -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.

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -76,30 +76,30 @@ defmodule TempleTest do
~s{<div><div id="static-child-1"></div>mitch<div id="static-child-2"></div></div>}
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{<div></div><span></span>}
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|<div id="hi"><div></div></div>|
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|<div id="hi"></div>|
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|<div id="hi"><div></div></div>|
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