Skip to content

Commit

Permalink
fix: for loop must access expression via value stack
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clarete committed Mar 18, 2021
1 parent a044918 commit 42be57b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 20 additions & 3 deletions templatel-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,29 @@ base-end")))))
'())
"b")))

(ert-deftest render-template-forloop ()
(ert-deftest render-template-for-loop ()
(should (equal
"One Two Three "
(templatel-render-string
"{% for name in names %}{{ name }} {% endfor %}"
'(("names" . ("One" "Two" "Three"))))
"One Two Three ")))
'(("names" . ("One" "Two" "Three")))))))

(ert-deftest render-template-for-loop-with-filter ()
(should (equal
"1 2 3 4 5 6 7 8 9 "
(templatel-render-string
"{% for i in a|sort %}{{ i }} {% endfor %}"
'(("a" . (3 6 1 4 9 5 8 2 7)))))))

(ert-deftest render-if-with-filter ()
(should
(equal
"test"
(templatel-render-string "{% if list | first %}test{% endif %}" '(("list" . (1))))))
(should
(equal
"test"
(templatel-render-string "{% if 1.6 | round > 1.4 %}test{% endif %}" '()))))

(ert-deftest render-if-else-elif-no-else ()
(should
Expand Down
8 changes: 7 additions & 1 deletion templatel.el
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ finished."
("min" . templatel-filters-min)
("round" . templatel-filters-round)
("safe" . templatel-filters-safe)
("sort" . templatel-filters-sort)
("sum" . templatel-filters-sum)
("title" . templatel-filters-title)
("upper" . templatel-filters-upper)
Expand Down Expand Up @@ -1598,7 +1599,9 @@ Otherwise its HTML entities are escaped."
"Compile for statement off TREE."
(let ((id (cdar tree)))
`(let ((subenv '((,id . nil)))
(iterable ,(templatel--compiler-run (cadr tree))))
(iterable (progn
,(templatel--compiler-run (cadr tree))
(pop rt/valstk))))
(push subenv rt/varstk)
(mapc
(lambda(id)
Expand Down Expand Up @@ -1808,6 +1811,9 @@ Hi <b>you</b>!
#+END_SRC"
(templatel-mark-safe s))

(defun templatel-filters-sort (s)
"Return S sorted."
(sort s #'<))



Expand Down

0 comments on commit 42be57b

Please sign in to comment.