Expose selmer.parser/resolve-arg (#1609)

This commit is contained in:
Michiel Borkent 2023-08-20 20:05:25 +02:00 committed by GitHub
parent f470dba671
commit 1881a75a1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View file

@ -16,6 +16,7 @@ A preview of the next release can be installed from
- [#1604](https://github.com/babashka/babashka/issues/1604): throw `FileNotFoundException` when requiring namespace whose file cannot be found (as JVM Clojure does) - [#1604](https://github.com/babashka/babashka/issues/1604): throw `FileNotFoundException` when requiring namespace whose file cannot be found (as JVM Clojure does)
- Bump integrant CI tests - Bump integrant CI tests
- [#1600](https://github.com/babashka/babashka/issues/1600): use pagesize of 64K on linux aarch64, so it works on Asahi linux - [#1600](https://github.com/babashka/babashka/issues/1600): use pagesize of 64K on linux aarch64, so it works on Asahi linux
- Expose `selmer.parser/resolve-arg`
## 1.3.182 (2023-07-20) ## 1.3.182 (2023-07-20)

View file

@ -4,6 +4,7 @@
[babashka.impl.common :refer [ctx]] [babashka.impl.common :refer [ctx]]
[sci.core :as sci] [sci.core :as sci]
[selmer.filters :as filters] [selmer.filters :as filters]
[selmer.filter-parser :as fp]
[selmer.parser] [selmer.parser]
[selmer.tags :as tags] [selmer.tags :as tags]
[selmer.util :as util] [selmer.util :as util]
@ -83,12 +84,31 @@
(apply merge) (apply merge)
(selmer.parser/render ~s))) (selmer.parser/render ~s)))
(defn resolve-arg
"Resolves an arg as passed to an add-tag! handler using the provided
context-map.
A custom tag handler will receive a seq of args as its first argument.
With this function, you can selectively resolve one or more of those args
so that if they contain literals, the literal value is returned, and if they
contain templates of any sort, which can itself have variables, filters or
tags in it, they will be returned resolved, applied and rendered.
Example:
(resolve-arg {{header-name|upper}} {:header-name \"My Page\"})
=> \"MY PAGE\""
[arg context-map]
(if (fp/literal? arg)
(fp/parse-literal arg)
(render arg context-map)))
(def selmer-parser-namespace (def selmer-parser-namespace
(-> selmer-parser-ns (-> selmer-parser-ns
(assoc 'render-file (sci/copy-var render-file spns) (assoc 'render-file (sci/copy-var render-file spns)
'render (sci/copy-var render spns) 'render (sci/copy-var render spns)
'render-template (sci/copy-var render-template spns) 'render-template (sci/copy-var render-template spns)
'resolve-var-from-kw (sci/copy-var resolve-var-from-kw spns) 'resolve-var-from-kw (sci/copy-var resolve-var-from-kw spns)
'resolve-arg (sci/copy-var resolve-arg spns )
'<< (sci/copy-var << spns)))) '<< (sci/copy-var << spns))))
(def stns (sci/create-ns 'selmer.tags nil)) (def stns (sci/create-ns 'selmer.tags nil))