Skip to content

Commit

Permalink
Signal error for {% if %} expressions without an {% else %}
Browse files Browse the repository at this point in the history
Issue #8
  • Loading branch information
mmontone committed May 10, 2023
1 parent fc5a071 commit 9815c31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
29 changes: 17 additions & 12 deletions compiler.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,24 @@
(coerce body 'list))
for output := (emit elem)
when output
collect output))))
collect output))))
(let ((exprs (read-template-expressions (code tag))))
(if (eql (first exprs) 'ten/template::section)
;; sections are a special case
(progn
;; push the section to the list of sections
;; to generate render-section methods later
(push (list (second exprs)
(emit-body (body tag)))
*sections*)
`(ten/compiler::render-section ',(second exprs) ten/template::%ten-template %ten-stream))
;; else
`(,@exprs ,@(emit-body (body tag)))))))
(case (first exprs)
(ten/template::section ;; sections are a special case
;; push the section to the list of sections
;; to generate render-section methods later
(push (list (second exprs)
(emit-body (body tag)))
*sections*)
`(ten/compiler::render-section ',(second exprs) ten/template::%ten-template %ten-stream))
(cl:if ;; check there's an else tag in body and emit
(when (not (find-if (lambda (x) (typep x 'ten/parser::<else-tag>))
(body tag)))
(error "Missing {% else %} in {% if %} expression: ~a"
tag))
`(,@exprs ,@(emit-body (body tag))))
(t ;; otherwise, just emit
`(,@exprs ,@(emit-body (body tag))))))))


(defun control-element-p (element)
Expand Down
17 changes: 17 additions & 0 deletions tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,20 @@
(is (not (null (search "&lt;div&gt;&lt;/div&gt;" (ten/examples:escaping2 "<div></div>")))))
(is (not (null (search "<div></div>" (ten/examples:escaping3 "<div></div>")))))
(is (not (null (search "<div></div>" (ten/examples:escaping4 "<div></div>"))))))

(def-test if-expression-test ()
;; {% if %} without {% else %} should fail to compile
(signals error
(ten:compile-template "{% template if-test-1 () (locale) %}
<html {% if locale %}lang=\"{{ locale }}\"{% end %}>
{% end %}"))
;; {% if %} with an {% else %} compiles
(finishes
(ten:compile-template "{% template if-test-2 () (locale) %}
<html {% if locale %}lang=\"{{ locale }}\"{% else %}{% end %}>
{% end %}"))
;; {% when %} does not need an else
(finishes
(ten:compile-template "{% template if-test-3 () (locale) %}
<html {% when locale %}lang=\"{{ locale }}\"{% end %}>
{% end %}")))

0 comments on commit 9815c31

Please sign in to comment.