Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aviflax opened this issue Apr 16, 2020 · 2 comments

Comments

@aviflax
Copy link
Contributor

aviflax commented Apr 16, 2020

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?

@yogthos
Copy link
Owner

yogthos commented Apr 16, 2020

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.

@unitof
Copy link

unitof commented Jul 8, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants