From c5c2bd89f1b9f0205a643bc60215f7c81ee8f5f6 Mon Sep 17 00:00:00 2001 From: MichaelHatherly Date: Sun, 8 Dec 2024 18:39:48 +0000 Subject: [PATCH] Add `@__once__` macro --- docs/src/index.md | 38 ++++++++++++++++++++++++ src/HypertextTemplates.jl | 2 ++ src/once.jl | 26 ++++++++++++++++ src/render-macro.jl | 7 +++-- test/references/basics/once-button-1.txt | 1 + test/references/basics/once-button-2.txt | 1 + test/references/basics/once-page.txt | 1 + test/runtests.jl | 38 ++++++++++++++++++++++++ 8 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/once.jl create mode 100644 test/references/basics/once-button-1.txt create mode 100644 test/references/basics/once-button-2.txt create mode 100644 test/references/basics/once-page.txt diff --git a/docs/src/index.md b/docs/src/index.md index 905d9ce..2578e23 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -127,6 +127,44 @@ end end ``` +## `@__once__` + +When you need to render HTML to a page only once per page, for example a JS +dependency that only needs including once via ` \ No newline at end of file diff --git a/test/references/basics/once-button-2.txt b/test/references/basics/once-button-2.txt new file mode 100644 index 0000000..930ff86 --- /dev/null +++ b/test/references/basics/once-button-2.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/references/basics/once-page.txt b/test/references/basics/once-page.txt new file mode 100644 index 0000000..3305e0b --- /dev/null +++ b/test/references/basics/once-page.txt @@ -0,0 +1 @@ +

Hello, World!

\ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index fd338e0..d6aacfe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -79,6 +79,32 @@ end end @deftag macro streaming end +@component function once_jquery() + @__once__ begin + @script {src = "https://code.jquery.com/jquery-3.6.0.min.js"} + end +end +@deftag macro once_jquery end + +@component function once_button() + @once_jquery + @button "Click Me" +end +@deftag macro once_button end + +@component function once_page() + @html begin + @head begin + @once_jquery + end + @body begin + @h1 "Hello, World!" + @once_button + end + end +end +@deftag macro once_page end + @testset "HypertestTemplates" begin @testset "Basics" begin render_test("references/basics/html-elements.txt") do io @@ -166,6 +192,18 @@ end @span {"x-show" := "open"} "Content..." end end + render_test("references/basics/once-button-1.txt") do io + @render io @once_button + end + render_test("references/basics/once-button-2.txt") do io + @render io begin + @once_button + @once_button + end + end + render_test("references/basics/once-page.txt") do io + @render io @once_page + end end @testset "Markdown" begin render_test("references/markdown/markdown.txt") do io