Skip to content

Commit

Permalink
Merge pull request #135 from tomconnors/selmer-third-arg
Browse files Browse the repository at this point in the history
Add separate templating functions for strings vs files
  • Loading branch information
retrogradeorbit authored Feb 24, 2022
2 parents f1161ae + dadff9e commit 936d20a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/clj/spire/namespaces.clj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
(quote ~'sudo) (sci-bind-macro sudo/sudo ~ns-sym)

(quote ~'selmer) (copy-var selmer/selmer ~ns-sym)
(quote ~'render-string) (copy-var selmer/render-string ~ns-sym)
(quote ~'render-file) (copy-var selmer/render-file ~ns-sym)

(quote ~'download*) (copy-var download/download* ~ns-sym)
(quote ~'download) (sci-bind-macro download/download ~ns-sym)
Expand Down
25 changes: 25 additions & 0 deletions src/clj/spire/selmer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,28 @@
source
(slurp (io/input-stream (io/file cwd source))))]
(parser/render pre-markup vars)))

(defn render-string
"Render a string template to a string.
Accepts the same arguments as `selmer.parser/render`"
[s context-map & [opts]]
(parser/render s context-map opts))

(defn render-file
"Render a template in a file to a string.
`file-path` is the relative path to the file to render.
`context-map` and `opts` are the same as those expected by `selmer.parser/render`"
[file-path context-map & [opts]]
(let [cwd (utils/current-file-parent)
pre-markup (slurp (io/input-stream (io/file cwd file-path)))]
(parser/render pre-markup context-map opts)))

(comment
(selmer "foo {{ bar }}" {:bar "qux"} :data)
;; paths are relative to spire repo in dev
(selmer "local/test-template" {:bar "qux"})

(render-string "foo {{ bar }}" {:bar "qux"})
(render-file "local/test-template" {:bar "qux"})

(render-string "foo [{ bar }]" {:bar "baz"} {:tag-open \[ :tag-close \]}))

0 comments on commit 936d20a

Please sign in to comment.