Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ A fast, [Django](https://docs.djangoproject.com/en/dev/ref/templates/builtins/)
[take](#take)
[drop](#drop)
[drop-last](#drop-last)
[get](#get)
[get-digit](#get-digit)
[hash](#hash)
[join](#join)
Expand Down Expand Up @@ -513,6 +514,8 @@ Similar to drop:

`(render "{{seq-of-some-sort|drop-last:4}}" {:seq-of-some-sort [:dog :cat :bird :bird :bird :is :the :word]})` =\> `[:dog :cat :bird :bird]`

#### get
`(render "{{my-map|get:@key}}" {:my-map {:a 1 :b 2} :key :b})` => `2`

#### get-digit
`(render "{{tis-a-number|get-digit:1}}" {:tis-a-number 12.34567})` => `7`
Expand Down
1 change: 1 addition & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* 1.12.next - [add `get` filter](https://github.com/yogthos/Selmer/issue/322)
* 1.12.69 - fix relfection warning
* 1.12.68 - fix handling nulls when parsing script tags
* 1.12.67 - fix reflection warning parsing uri
Expand Down
9 changes: 6 additions & 3 deletions src/selmer/filters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ map. The rest of the arguments are optional and are always strings."
(-> (.getTime ^java.sql.Time d)
(Instant/ofEpochMilli)
(LocalDateTime/ofInstant (ZoneId/systemDefault)))

(instance? java.sql.Timestamp d)
(-> (.getTime ^java.sql.Timestamp d)
(Instant/ofEpochMilli)
Expand All @@ -70,7 +70,7 @@ map. The rest of the arguments are optional and are always strings."
(-> (.toInstant ^java.util.Date d)
(.atZone (ZoneId/systemDefault))
(.toLocalDateTime))

(instance? java.time.Instant d)
(-> (.atZone ^java.time.Instant d (ZoneId/systemDefault))
(.toLocalDateTime))
Expand Down Expand Up @@ -553,7 +553,10 @@ map. The rest of the arguments are optional and are always strings."
(throw (Exception. (str number " does not appear to be a valid phone number"))))))

:name
name}))
name

:get
get}))


(defn get-filter
Expand Down
15 changes: 15 additions & 0 deletions test/selmer/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,21 @@
(is (= "quux" ;; test nested lookup
(render "{{name|default:@foo.bar.baz}}" {:name nil :foo {:bar {:baz "quux"}}}))))

(deftest filter-get-tests
(is (= "bar"
(render "{{data|get:\"foo\"}}" {:data {"foo" "bar" "baz" "quux"}})))
(is (= "quux"
(render "{{data|get:\"baz\"}}" {:data {"foo" "bar" "baz" "quux"}})))
(is (= ""
(render "{{data|get:\"nonexistent\"}}" {:data {"foo" "bar" "baz" "quux"}})))
(is (= "default-value"
(render "{{data|get:\"nonexistent\":\"default-value\"}}" {:data {"foo" "bar" "baz" "quux"}})))
(is (= "2"
(render "{{my-map|get:@key}}" {:my-map {:a 1 :b 2} :key :b})))
(let [data {:x {"a" 1 "b" 2} :ys ["a" "b"]}]
(is (= "12"
(render "{% for y in ys %}{{x|get:@y}}{% endfor %}" data)))))

(deftest custom-resource-path-setting
(is (nil? *custom-resource-path*))
(do
Expand Down
Loading