Add @props access to components (#66)
* Add @props access to components * Document `@props` assign
This commit is contained in:
parent
916a9469d6
commit
fa41e73bb0
4 changed files with 35 additions and 1 deletions
|
@ -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.
|
||||
This works exactly the same as EEx templates. The whole list or map of assigns can be accessed by the special `@props` assign.
|
||||
|
||||
## Children
|
||||
|
||||
|
|
|
@ -39,6 +39,10 @@ defmodule Temple.Utils do
|
|||
inner
|
||||
end
|
||||
|
||||
def insert_props({:@, _, [{:props, _, _}]}, props, _) do
|
||||
props
|
||||
end
|
||||
|
||||
def insert_props({:@, _, [{name, _, _}]}, props, _) when is_atom(name) do
|
||||
quote location: :keep do
|
||||
Access.get(unquote_splicing([props, name]))
|
||||
|
|
|
@ -13,6 +13,10 @@ defmodule Component do
|
|||
end
|
||||
end
|
||||
|
||||
defcomponent :lists_props do
|
||||
partial inspect(@props) |> Phoenix.HTML.raw()
|
||||
end
|
||||
|
||||
defcomponent :arbitrary_code do
|
||||
num = 1..10 |> Enum.reduce(0, fn x, sum -> x + sum end)
|
||||
|
||||
|
|
|
@ -76,6 +76,32 @@ 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
|
||||
import Component
|
||||
|
||||
props = [foo: "bar", hello: "world"]
|
||||
|
||||
{:safe, result} =
|
||||
temple do
|
||||
lists_props(props)
|
||||
end
|
||||
|
||||
assert result == inspect(props)
|
||||
end
|
||||
|
||||
test "can access props map" do
|
||||
import Component
|
||||
|
||||
props = %{foo: "bar", hello: "world"}
|
||||
|
||||
{:safe, result} =
|
||||
temple do
|
||||
lists_props(props)
|
||||
end
|
||||
|
||||
assert result == inspect(props)
|
||||
end
|
||||
|
||||
test "can have arbitrary code inside the definition" do
|
||||
import Component
|
||||
|
||||
|
|
Reference in a new issue