Skip to content

Commit

Permalink
* add head configuration so users can add extra script tags
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco committed May 6, 2024
1 parent 34f15bd commit 6723de7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/francis_htmx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ defmodule FrancisHtmx do
@spec htmx((Plug.Conn.t() -> binary()), Keyword.t()) :: Macro.t()
defmacro htmx(content, opts \\ []) do
title = Keyword.get(opts, :title, "")
head = Keyword.get(opts, :head, "")

quote location: :keep do
get("/", fn conn ->
"""
<!DOCTYPE html>
<html>
<head>
<title>#{unquote(title)}</title>
#{unquote(head)}
<script src="https://unpkg.com/htmx.org/dist/htmx.js"></script>
<title>#{unquote(title)}</title>
</head>
<body>
#{unquote(content).(conn)}
Expand Down
30 changes: 24 additions & 6 deletions test/francis_htmx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ defmodule FrancisHtmxTest do

assert html
|> Floki.find("script")
|> Floki.attribute("src") == ["https://unpkg.com/htmx.org/dist/htmx.js"]
|> Floki.attribute("src") == [
"https://cdn.tailwindcss.com",
"https://unpkg.com/htmx.org/dist/htmx.js"
]

assert html
|> Floki.find("link")
|> Floki.attribute("href") == ["/app.css"]

assert html
|> Floki.find("title")
|> Floki.text() == "Testing HTMX"

assert html
|> Floki.find("body")
Expand All @@ -23,11 +34,18 @@ defmodule FrancisHtmxTestHandler do
use Francis
import FrancisHtmx

htmx(fn _ ->
assigns = %{title: "test"}
htmx(
fn _ ->
assigns = %{title: "test"}

~E"""
<div><%= @title %></div>
~E"""
<div><%= @title %></div>
"""
end,
title: "Testing HTMX",
head: """
<script src="https://cdn.tailwindcss.com"></script>
<link href="/app.css" rel="stylesheet">
"""
end)
)
end

0 comments on commit 6723de7

Please sign in to comment.