Skip to content

Commit

Permalink
Merge pull request #199 from pepe/temple-capture
Browse files Browse the repository at this point in the history
Add capture* functions to temple module
  • Loading branch information
bakpakin authored Sep 24, 2024
2 parents d015a35 + 8168ba7 commit a6f7fe9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
23 changes: 19 additions & 4 deletions spork/temple.janet
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,25 @@
[path &]
(with-dyns [:current-file path]
(let [tmpl (create (slurp path) path)]
@{'render @{:doc "Main template function."
:value (fn render [&keys args] (tmpl args))}
'render-dict @{:doc "Template function, but pass arguments as a dictionary."
:value tmpl}})))
@{'render
@{:doc "Main template function."
:value (fn render [&keys args] (tmpl args))}
'render-dict
@{:doc "Template function, but pass arguments as a dictionary."
:value tmpl}
'capture
@{:doc "Template function that returns buffer of rendered template."
:value (fn capture [&keys args]
(def b @"")
(with-dyns [:out b] (tmpl args))
b)}
'capture-dict
@{:doc "Template function that returns buffer of rendered template,
but pass arguments as a dictionary."
:value (fn capture-dict [args]
(def b @"")
(with-dyns [:out b] (tmpl args))
b)}})))

(defn add-loader
"Adds the custom template loader to Janet's module/loaders and
Expand Down
3 changes: 3 additions & 0 deletions test/suite-temple.janet
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
</html>
```)

(assert (deep= (hi/capture-dict {:a 1 :b 2})
(hi/capture :a 1 :b 2)))

(def str-template "<html>hello {{ (args :arg) }}</html>")
(def render (temple/compile str-template))
(def out (render :arg "world"))
Expand Down

0 comments on commit a6f7fe9

Please sign in to comment.