Skip to content

Commit

Permalink
Issue #133, String keys in for loops (#147)
Browse files Browse the repository at this point in the history
* fix issue #133

* improved performance and reuse an older function

* Added some tests
  • Loading branch information
lucacervello authored and yogthos committed Mar 24, 2017
1 parent 587ae6a commit c6524ba
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/selmer/filter_parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ applied filter."
val
(map vector filter-strs filters)))

(defn- get-accessor [m k]
(defn get-accessor [m k]
"Returns the value of `k` from map `m`, either as a keyword or string lookup."
(or (get m k)
(when (keyword? k)
Expand Down
4 changes: 2 additions & 2 deletions src/selmer/tags.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns selmer.tags
(:require selmer.node
[selmer.filter-parser :refer [split-filter-val safe-filter compile-filter-body fix-accessor]]
[selmer.filter-parser :refer [split-filter-val safe-filter compile-filter-body fix-accessor get-accessor]]
[selmer.filters :refer [filters]]
[selmer.util :refer :all]
[json-html.core :refer [edn->html]])
Expand Down Expand Up @@ -44,7 +44,7 @@
item-keys (parse-arg items)]
(fn [context-map]
(let [buf (StringBuilder.)
items (-> (get-in context-map item-keys)
items (-> (reduce get-accessor context-map item-keys)
(apply-filters filters context-map items))
length (count items)]
(if (and empty-content (empty? items))
Expand Down
6 changes: 5 additions & 1 deletion test/selmer/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@
"<<1>><<2>><<3>>"))
(is (= (render "{% for ele in foo %}{{ele}}-{{forloop.counter}}-{{forloop.counter0}}-{{forloop.revcounter}}-{{forloop.revcounter0}};{%endfor%}"
{:foo [1 2 3]})
"1-1-0-2-3;2-2-1-1-2;3-3-2-0-1;")))
"1-1-0-2-3;2-2-1-1-2;3-3-2-0-1;"))
(is (= (render "{% for ele in foo %}{{ele.bar}} {% endfor %}"
{"foo" [{:bar "bar"}
{:bar "bar"}]})
"bar bar ")))

(deftest for-filter-test
(is
Expand Down

0 comments on commit c6524ba

Please sign in to comment.