Skip to content

Commit 996d7de

Browse files
committed
feat: pass values from page through to layouts
1 parent a49eb84 commit 996d7de

File tree

6 files changed

+31
-39
lines changed

6 files changed

+31
-39
lines changed

lib/tableau/document.ex

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ defmodule Tableau.Document do
66
77
Please see the `Tableau.Layout` docs for more info.
88
"""
9-
defmacro render(inner_content, extra_assigns \\ Macro.escape(%{})) do
9+
defmacro render(inner_content) do
1010
quote do
1111
case unquote(inner_content) do
12-
[module | rest] ->
13-
module.template(
14-
Map.merge(Map.new(unquote(extra_assigns)), %{
15-
site: Access.fetch!(var!(assigns), :site),
16-
inner_content: rest
17-
})
18-
)
12+
[{module, page_assigns} | rest] ->
13+
module.template(Map.merge(page_assigns, %{inner_content: rest}))
1914

2015
[] ->
2116
nil
@@ -36,6 +31,10 @@ defmodule Tableau.Document do
3631
raise "Failed to find layout path for #{inspect(module)}"
3732
end
3833

39-
root.template(Map.merge(assigns, %{inner_content: mods}))
34+
page_assigns = Map.new(module.__tableau_extra__() || [])
35+
mods = for mod <- mods, do: {mod, page_assigns}
36+
new_assigns = Map.merge(page_assigns, %{inner_content: mods})
37+
38+
root.template(Map.merge(assigns, new_assigns))
4039
end
4140
end

lib/tableau/page.ex

+8-21
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,15 @@ defmodule Tableau.Page do
3232
@callback template(assigns()) :: template()
3333

3434
defmacro __using__(opts) do
35-
opts = Keyword.validate!(opts, [:layout, :permalink])
35+
opts = Keyword.validate!(opts, [:layout, :permalink, extra: []])
3636

37-
page =
38-
quote do
39-
def __tableau_type__, do: :page
40-
end
37+
quote do
38+
@behaviour unquote(__MODULE__)
4139

42-
parent =
43-
quote do
44-
def __tableau_parent__, do: unquote(opts[:layout])
45-
end
46-
47-
permalink =
48-
quote do
49-
def __tableau_permalink__, do: unquote(opts[:permalink])
50-
end
51-
52-
postlude =
53-
quote do
54-
@behaviour unquote(__MODULE__)
55-
end
56-
57-
[page, parent, permalink, postlude]
40+
def __tableau_type__, do: :page
41+
def __tableau_parent__, do: unquote(opts[:layout])
42+
def __tableau_permalink__, do: unquote(opts[:permalink])
43+
def __tableau_extra__, do: unquote(opts[:extra])
44+
end
5845
end
5946
end

test/mix/tasks/tableau.build_test.exs

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ defmodule Mix.Tasks.Tableau.BuildTest.About do
2424
def __tableau_type__, do: :page
2525
def __tableau_parent__, do: InnerLayout
2626
def __tableau_permalink__, do: "/about"
27+
def __tableau_extra__, do: []
2728

2829
EEx.function_from_string(
2930
:def,
3031
:template,
3132
~g'''
32-
<div class="<%= @class %>">
33+
<div>
3334
hi
3435
</div>
3536
'''html,
36-
[:assigns]
37+
[:_assigns]
3738
)
3839
end
3940

@@ -45,6 +46,7 @@ defmodule Mix.Tasks.Tableau.BuildTest.Index do
4546
def __tableau_type__, do: :page
4647
def __tableau_parent__, do: InnerLayout
4748
def __tableau_permalink__, do: "/"
49+
def __tableau_extra__, do: []
4850

4951
EEx.function_from_string(
5052
:def,
@@ -60,7 +62,7 @@ end
6062

6163
defmodule Mix.Tasks.Tableau.BuildTest.InnerLayout do
6264
import Tableau.Strung
63-
import Tableau.Document.Helper, only: [render: 2]
65+
import Tableau.Document.Helper, only: [render: 1]
6466
require EEx
6567
alias Mix.Tasks.Tableau.BuildTest.RootLayout
6668

@@ -72,7 +74,7 @@ defmodule Mix.Tasks.Tableau.BuildTest.InnerLayout do
7274
:template,
7375
~g'''
7476
<div id="inner-layout">
75-
<%= render(@inner_content, class: "text-red") %>
77+
<%= render(@inner_content) %>
7678
</div>
7779
'''html,
7880
[:assigns]

test/tableau/document_test.exs

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ defmodule Tableau.DocumentTest.About do
66
def __tableau_type__, do: :page
77
def __tableau_parent__, do: InnerLayout
88
def __tableau_permalink__, do: "/about"
9+
def __tableau_extra__, do: [yo: "lo"]
910

1011
EEx.function_from_string(
1112
:def,
1213
:template,
1314
~g'''
14-
<div class="<%= @class %>">
15+
<div id="<%= @yo %>">
1516
hi
1617
</div>
1718
'''html,
@@ -27,6 +28,7 @@ defmodule Tableau.DocumentTest.Index do
2728
def __tableau_type__, do: :page
2829
def __tableau_parent__, do: InnerLayout
2930
def __tableau_permalink__, do: "/"
31+
def __tableau_extra__, do: []
3032

3133
EEx.function_from_string(
3234
:def,
@@ -42,7 +44,7 @@ end
4244

4345
defmodule Tableau.DocumentTest.InnerLayout do
4446
import Tableau.Strung
45-
import Tableau.Document.Helper, only: [render: 2]
47+
import Tableau.Document.Helper, only: [render: 1]
4648
require EEx
4749
alias Tableau.DocumentTest.RootLayout
4850

@@ -54,7 +56,7 @@ defmodule Tableau.DocumentTest.InnerLayout do
5456
:template,
5557
~g'''
5658
<div id="inner-layout">
57-
<%= render(@inner_content, class: "text-red") %>
59+
<%= render(@inner_content) %>
5860
</div>
5961
'''html,
6062
[:assigns]
@@ -98,8 +100,7 @@ defmodule Tableau.DocumentTest do
98100
{"head", [], []},
99101
{"body", [],
100102
[
101-
{"div", [{"id", "inner-layout"}],
102-
[{"div", [{"class", "text-red"}], ["\n hi\n"]}]}
103+
{"div", [{"id", "inner-layout"}], [{"div", [{"id", "lo"}], ["\n hi\n"]}]}
103104
]}
104105
]}
105106
]

test/tableau/graph/node_test.exs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Tableau.Graph.NodeTest do
99
def __tableau_type__, do: :page
1010
def __tableau_parent__, do: Layout
1111
def __tableau_permalink__, do: "/about"
12+
def __tableau_extra__, do: []
1213

1314
def template(_) do
1415
""

test/tableau/graph_test.exs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule Tableau.GraphTest do
77
def __tableau_type__, do: :page
88
def __tableau_parent__, do: InnerLayout
99
def __tableau_permalink__, do: "/about"
10+
def __tableau_extra__, do: []
1011

1112
def template(_), do: ""
1213
end
@@ -17,6 +18,7 @@ defmodule Tableau.GraphTest do
1718
def __tableau_type__, do: :page
1819
def __tableau_parent__, do: RootLayout
1920
def __tableau_permalink__, do: "/about"
21+
def __tableau_extra__, do: []
2022
def template(_), do: ""
2123
end
2224

0 commit comments

Comments
 (0)