diff --git a/CHANGELOG.md b/CHANGELOG.md index ec0d8831..7ea0cff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) - 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 +- Expose `selmer.parser/resolve-arg` ## 1.3.182 (2023-07-20) diff --git a/feature-selmer/babashka/impl/selmer.clj b/feature-selmer/babashka/impl/selmer.clj index 20c8416a..155c8bed 100644 --- a/feature-selmer/babashka/impl/selmer.clj +++ b/feature-selmer/babashka/impl/selmer.clj @@ -4,6 +4,7 @@ [babashka.impl.common :refer [ctx]] [sci.core :as sci] [selmer.filters :as filters] + [selmer.filter-parser :as fp] [selmer.parser] [selmer.tags :as tags] [selmer.util :as util] @@ -83,12 +84,31 @@ (apply merge) (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 (-> selmer-parser-ns (assoc 'render-file (sci/copy-var render-file spns) 'render (sci/copy-var render spns) 'render-template (sci/copy-var render-template 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)))) (def stns (sci/create-ns 'selmer.tags nil))