This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-17
Channels
- # ai (1)
- # announcements (2)
- # babashka (29)
- # beginners (43)
- # calva (1)
- # cider (3)
- # clerk (6)
- # clj-kondo (3)
- # cljdoc (2)
- # cljs-dev (1)
- # clojure (16)
- # community-development (8)
- # data-science (1)
- # emacs (6)
- # events (4)
- # hyperfiddle (14)
- # lsp (24)
- # practicalli (1)
- # reagent (5)
- # releases (1)
- # rewrite-clj (2)
- # vim (6)
Anyone with Selmer experience, any way to do a double-format or number-format but not throw if nil? Like can I combine filters maybe?
one way to do that would be with an if tag:
(s/render
"b: {% if b%}{{b|double-format}}{% endif %}
c: {% if c%}{{c|double-format}}{% endif %}"
{:b nil :c 42})
=> "b: \nc: 42.0"
What do you intend to do in the case of nil? I was thinking if tag as well (but I'm new to Selmer so I had to try it out and got beat to it 😄), but if you have something fairly particular and don't want the verbosity of an if/else, you could try registering your own function as a filter to do the work
and if there's something you'd like to use if the value is nil, you could add a filter:
(s/add-filter! :or-zero (fnil identity 0))
(s/render
"b: {{b|or-zero|double-format}}
c: {{c|or-zero|double-format}}"
{:b nil :c 42})
=> "b: 0.0\nc: 42.0"
You win @U013JFLRFS8 🙃
Interesting... I was trying to combine filters, but it wasn't working, but I see now why. I thought something like
{{num|default:0|double-format}}
But it doesn't work with {:num nil}
as an input because default
returns a string which blows up double-format
It would be nice if there was a way to short the chained filter, say if a filter returned a reduced value. So you could have a default short circuit if nil and return the default value for example.
@U013JFLRFS8 I saw the thread but was going to respond with the if
tag too 🙂 Since filters work on values, the only way to get a double-format-or-nil filter is to write it yourself, depending on what semantics you want -- they're simple value transforms, not higher-order functions.
what I've done in similar situations is prepped my data before passing it to render - I try to view the templating engine as a string substitution mechanism, and severely limit how much conditional logic I put into a template - that's what clojure is for (IMO)
<!-- (Just to make this conversation legal in the clojure channel, I'll mention that) Enlive provides splendiferous HTML template-filling, with the full power of Clojure. In contrast to "Easy" solutions like the one under discussion, Enlive is "Simple". -->
Hi all 👋 I am revisiting Clojure after a few years, and I’m catching up. I recall the conj 2018 talk Maybe Not on key sets as domain spaces, and the questions Rich raised around when optionality in specs should occur. Whatever came of the hypothetical caller syntax at the end of the talk? Is there an answer in 1.11.x for the “when question”?
As far as I know, work on Spec 2 continues, but there's not a clear target for when it will become final and public. If you're interested in community efforts aligned with the spirit of spec, I'd recommend you check out the excellent #malli library. I don't know specifically how it addresses your question about caller syntax (it's been a while since I've watched that talk), but you will probably find it interesting and useful. https://github.com/metosin/malli
This looks fantastic, thanks @UFTRLDZEW!