Skip to content

Commit 42be57b

Browse files
committed
fix: for loop must access expression via value stack
The compilation of for loop statements was accessing the expression that evaluates what collection it will iterate over via a recursive call to the compiler, and that only worked for other cases by luck. In order to test this feature, I've added a new filter: `sort`, it doesn't take any arguments for now but could be a bit more flexible. Refs: #11
1 parent a044918 commit 42be57b

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

templatel-tests.el

+20-3
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,29 @@ base-end")))))
762762
'())
763763
"b")))
764764

765-
(ert-deftest render-template-forloop ()
765+
(ert-deftest render-template-for-loop ()
766766
(should (equal
767+
"One Two Three "
767768
(templatel-render-string
768769
"{% for name in names %}{{ name }} {% endfor %}"
769-
'(("names" . ("One" "Two" "Three"))))
770-
"One Two Three ")))
770+
'(("names" . ("One" "Two" "Three")))))))
771+
772+
(ert-deftest render-template-for-loop-with-filter ()
773+
(should (equal
774+
"1 2 3 4 5 6 7 8 9 "
775+
(templatel-render-string
776+
"{% for i in a|sort %}{{ i }} {% endfor %}"
777+
'(("a" . (3 6 1 4 9 5 8 2 7)))))))
778+
779+
(ert-deftest render-if-with-filter ()
780+
(should
781+
(equal
782+
"test"
783+
(templatel-render-string "{% if list | first %}test{% endif %}" '(("list" . (1))))))
784+
(should
785+
(equal
786+
"test"
787+
(templatel-render-string "{% if 1.6 | round > 1.4 %}test{% endif %}" '()))))
771788

772789
(ert-deftest render-if-else-elif-no-else ()
773790
(should

templatel.el

+7-1
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,7 @@ finished."
12381238
("min" . templatel-filters-min)
12391239
("round" . templatel-filters-round)
12401240
("safe" . templatel-filters-safe)
1241+
("sort" . templatel-filters-sort)
12411242
("sum" . templatel-filters-sum)
12421243
("title" . templatel-filters-title)
12431244
("upper" . templatel-filters-upper)
@@ -1598,7 +1599,9 @@ Otherwise its HTML entities are escaped."
15981599
"Compile for statement off TREE."
15991600
(let ((id (cdar tree)))
16001601
`(let ((subenv '((,id . nil)))
1601-
(iterable ,(templatel--compiler-run (cadr tree))))
1602+
(iterable (progn
1603+
,(templatel--compiler-run (cadr tree))
1604+
(pop rt/valstk))))
16021605
(push subenv rt/varstk)
16031606
(mapc
16041607
(lambda(id)
@@ -1808,6 +1811,9 @@ Hi <b>you</b>!
18081811
#+END_SRC"
18091812
(templatel-mark-safe s))
18101813

1814+
(defun templatel-filters-sort (s)
1815+
"Return S sorted."
1816+
(sort s #'<))
18111817

18121818

18131819

0 commit comments

Comments
 (0)