diff --git a/spork/temple.janet b/spork/temple.janet index 57224e1..f3c2e40 100644 --- a/spork/temple.janet +++ b/spork/temple.janet @@ -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 diff --git a/test/suite-temple.janet b/test/suite-temple.janet index 9aa921b..8a9fecd 100644 --- a/test/suite-temple.janet +++ b/test/suite-temple.janet @@ -33,6 +33,9 @@ ```) +(assert (deep= (hi/capture-dict {:a 1 :b 2}) + (hi/capture :a 1 :b 2))) + (def str-template "hello {{ (args :arg) }}") (def render (temple/compile str-template)) (def out (render :arg "world"))