Selmer icon indicating copy to clipboard operation
Selmer copied to clipboard

When the alt value passed to `default` is a lookup that isn’t found, the filter returns the text of the lookup

Open aviflax opened this issue 5 years ago • 2 comments

Reproduction:

=> (sp/render "{{ props.docs|default:@props.notes }}"
              {:props {}})
"@props.notes"

In the templates I’m writing, I had expected the filter to return either nil or a blank string.

@yogthos I’m not sure if we should consider this a bug or a feature request, but either way, what do you think?

aviflax avatar Apr 16 '20 19:04 aviflax

Unfortunately, this is a bit of an edge case because the current behavior is designed to allow @ symbol to be specified in a default literal. What happens specifically is this:

(defn lookup-args
  "Given a context map, return a function that accepts a filter
  argument and if it begins with @, return the value from the
  context map instead of treating it as a literal."
  [context-map]
  (fn [^String arg]
    (if (and (> (count arg) 1) (.startsWith arg "@"))
      (let [accessor (parse-accessor (subs arg 1))]
        (get-in context-map accessor arg))
      arg)))

The lookup-args function tries to find the path in the context @props.notes, and since the path doesn't exist it assumes the argument was a literal.

yogthos avatar Apr 16 '20 20:04 yogthos

@aviflax Why the need to dereference props.notes with @?

(Asking because I'm genuinely curious, and learning Clojure.)

Wouldn't

=> (sp/render "{{ props.docs|default:props.notes }}"
              {:props {}})

function as you're expecting?

unitof avatar Jul 08 '20 03:07 unitof