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

Add hook to call on missing properties #1435

Open
3 tasks done
JuergenSimon opened this issue Apr 3, 2018 · 11 comments
Open
3 tasks done

Add hook to call on missing properties #1435

JuergenSimon opened this issue Apr 3, 2018 · 11 comments
Labels
Milestone

Comments

@JuergenSimon
Copy link

Before filing issues, please check the following points first:

Check out the jsfiddle at https://jsfiddle.net/fwrww8tj. What is demonstrated there is this problem: when registering a helper for catching missing keys in the data using:

Handlebars.registerHelper("helperMissing", function(context, options) { ... })

the helper function is called correctly when keys are missing on the top-level, but fails when the key is down the path in the data somewhere. Consider the following data set:

{
    foo: "foo",
    nested_property: {
        foo: "foo"
    }
}

When the template calls for {{bar}}, the function is correctly called. However, when the template calls for {{nested_property.bar}}, the helper is NOT called.

@nknapp
Copy link
Collaborator

nknapp commented Apr 3, 2018

nested_property.bar is not a helper call, but a property access. When accessing top-level properties, there is no syntactical distinction between helpers and property access, but helpers cannot contain a ".", as far as I know. If you need to know if the property is there, you have to use "#if" or "#unless". I don't think there is a generic fallback mechanism for missing properties. " helperMissing" is for helpers that are not registered.

@mkistler
Copy link

I also hit this problem. Obviously there is some code that processes this case because the "property" is simply replaced with the empty string. It would be really nice if we could hook that processing and control what is produced.

@jimmyn
Copy link

jimmyn commented Sep 9, 2019

I'm having the same issue, I need to warn a user that property does not exist on some object but so far I haven't found a way to do that

@nknapp
Copy link
Collaborator

nknapp commented Sep 9, 2019

Changing this probably reaches down into compiler. I am not sure what exactly needs to be changed.

@wycats What do you think of this? A hook to be called when nested properties don't exist? I'm not going to work on this myself currently, but I would like to know if you agree to merge a PR if someone wants to create one.

@jimmyn
Copy link

jimmyn commented Sep 23, 2019

@nknapp @wycats any news on this? Maybe you can suggest some workaround?

@nknapp
Copy link
Collaborator

nknapp commented Sep 24, 2019

no, it makes sense, but I haven't got the time to work on it and it seems like @wycats is also busy.

Besides, we want a spec (#1277) before adding new features. Before I start considering it, I want @wycats approval, because he explicitly asked me only to fix bugs and not to add new features until we have a spec.

The only workaround that I can think of, involves proxies. You could probably create a proxy that throws an exception then trying the retrieve an non-existing object. But it would not work in Internet Explorer and you'd have to work out the internals yoursel.

@nknapp nknapp changed the title Missing keys in JSON path do not trigger helper function Add hook to call on missing properties Apr 5, 2020
@nknapp nknapp added the feature label Apr 5, 2020
@nknapp nknapp added this to the Backlog milestone Apr 5, 2020
@kohlerdominik
Copy link

Three years and counting :)
I think it would be a good addiction to provide a propertyMissing hook.

I want to share our usecase for this:
We provide a TemplateEditor, shown as HTML/Mustache-Input on the left side and a rendered preview on the right side.
It's easier to use if the user has not to provide a context while writing the template, so we'd like to render the property names instead of the property values in the template.

What we would like to see is something like:

Handlebars.registerPropertyMissingHandler(function(name) { 
  return "{{" + name + "}}"
})

@stevemk14ebr
Copy link

strict mode doesn't properly throw when dotted lookups occur. This breaks my workflow when rendering templates, expecting ones with missing fields/properties to fail.

HandleBars.compile(t, { strict: true })
{#each rollup}} | `{{{this.[original filename]}}}` | `{{{this.size}}}` |
{{/each}}

@nliu-ms
Copy link

nliu-ms commented Jul 4, 2022

Any update on this? It's a valuable feature for us as well.

@Mupli
Copy link

Mupli commented Sep 10, 2022

still no update on this ?? lol

@misabel
Copy link

misabel commented Oct 18, 2023

+1 to this being a valuable feature addition

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

No branches or pull requests

9 participants