Fork me on GitHub
#clojure
<
2023-06-17
>
didibus03:06:31

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?

Bob B03:06:03

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"

Jason Bullers04:06:57

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

Bob B04:06:21

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"

2
Bob B04:06:56

must be off-hours - Sean or Alex usually get in before me 🙂

😂 2
gratitude-thank-you 2
Jason Bullers04:06:56

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

Bob B04:06:07

that was also the first thing I tried

didibus04:06:45

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.

seancorfield05:06:00

@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.

Bob B06:06:14

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)

4
phill11:06:30

<!-- (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". -->

damien14:06:05

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”?

👋 4
respatialized15:06:44

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

damien16:06:00

This looks fantastic, thanks @UFTRLDZEW!