Skip to content

Commit

Permalink
update example with static; htmx now uses function to load code easily
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed Nov 16, 2023
1 parent c317a83 commit c4e8449
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
33 changes: 9 additions & 24 deletions example/lib/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@ defmodule Example do
use Francis
import FrancisHtmx

htmx("""
<style>
.smooth { transition: all 1s ease-in; font-size: 8rem;}
</style>
<div
id="color-demo"
class="smooth"
style="color:red"
hx-get="/colors"
hx-swap="outerHTML"
hx-trigger="every 1s">
Color Swap Demo
</div>
""")
htmx(fn _conn -> File.read!("static/root.html") end)

get("/colors", fn _ ->
new_color = 3 |> :crypto.strong_rand_bytes() |> Base.encode16() |> then(&"##{&1}")
new_color =
3
|> :crypto.strong_rand_bytes()
|> Base.encode16()
|> then(&"##{&1}")

"""
<div
id="color-demo"
class="smooth"
style="color:#{new_color}"
hx-get="/colors"
hx-swap="outerHTML"
hx-trigger="every 1s">
<p id="color-demo" class="smooth" style="color:#{new_color}">
Color Swap Demo
</div>
</p>
"""
end)

end
2 changes: 1 addition & 1 deletion example/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Example.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:francis_htmx, "~> 0.0.1"}
{:francis_htmx, path: "../"}
]
end
end
10 changes: 10 additions & 0 deletions example/static/root.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<style>
.smooth {
transition: all 1s ease-in;
font-size: 8rem;
}
</style>
<div hx-get="/colors" hx-trigger="every 1s">
<p id="color-demo" class="smooth">Color Swap Demo</p>
<p></p>
</div>
7 changes: 3 additions & 4 deletions lib/francis_htmx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ defmodule FrancisHtmx do
unmatched(fn _ -> "not found" end)
end
end

defmacro htmx(content, opts \\ []) do
defmacro htmx(content, opts\\[]) do
quote location: :keep do
get("/", fn _conn -> unquote(root(content, opts)) end)
get("/", fn conn -> root(unquote(content).(conn), unquote(opts)) end)
end
end

defp root(content, opts) do
def root(content, opts) when is_binary(content) do
title = Keyword.get(opts, :title, "")

"""
Expand Down
Empty file removed lib/root.html
Empty file.

0 comments on commit c4e8449

Please sign in to comment.