Selmer icon indicating copy to clipboard operation
Selmer copied to clipboard

No boolean operators for conditions

Open gerdint opened this issue 1 year ago • 6 comments

I know there is "all" and "any" in conditions, but there doesn't seem to be an "and" (or "or") operator for instance?

Like: {% if condition-a and condition-b %} (or perhaps "if and conditon-a condition-b")

Right now I need to nest two if-tags, which is not so nice..

gerdint avatar Oct 25 '24 15:10 gerdint

It shouldn't be too hard to add that in, the relevant code is here https://github.com/yogthos/Selmer/blob/a535a5c55e078288e5da58b6be5e1ae1b744b3c1/src/selmer/tags.clj#L85

Any chance you'd be up to do a PR for this? :)

yogthos avatar Oct 25 '24 15:10 yogthos

Yeah guess not. In my case I also need nested conditions, like:

{% if (not condition-a) and condition-b %}

is there support for nested conditions right now? It does not seem like it. It's basically just about a general expression evaluator.

gerdint avatar Oct 25 '24 16:10 gerdint

Seems like even the Django template language supports this?

https://www.geeksforgeeks.org/boolean-operators-django-template-tags/

gerdint avatar Oct 25 '24 16:10 gerdint

Nested conditions would be a bit trickier as it'd need a bit of a redesign as I recall. The original idea was to provide fairly minimal support for logic as you can always do fancier transforms in Clojure before passing the data to Selmer.

yogthos avatar Oct 25 '24 16:10 yogthos

All right. Well just one level deep conditions (but ideally including support for not operator in subexpressions) would perhaps be a good compromise (and be on par with Django's language). But unless I get quite bored in the near future I have to admit this is not a high priority for me, and my current project is a one man show where I much prefer Hiccup anyway. And as you say there is always the possibility to do more logic in Clojure behind the scenes.

gerdint avatar Oct 25 '24 17:10 gerdint

Yeah, I find Hiccup is generally preferable as well. The main use for Selmer is to provide something familiar for people coming from other languages. It also can be useful outside HTML templating since it's agnostic regarding the content it processes.

yogthos avatar Oct 25 '24 17:10 yogthos