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

Is it possible to return a custom string if the key is missing from context? #318

Closed
alindsay55661 opened this issue Sep 19, 2012 · 2 comments
Labels

Comments

@alindsay55661
Copy link

Rather than having nothing come back, I would like to generate the key wrapped in braces. For example this context:

{ foo: 'foo' }

And this template:

<div>{{foo}}</div>
<div>{{bar}}</div>

Should render this output:

<div>foo</div>
<div>{{bar}}</div>

// or this...
<div>foo</div>
<div>[[bar]]</div>

// or this...
<div>foo</div>
<div>{{missing key - bar}}</div>

instead of this:

<div>foo</div>
<div></div>

The idea is to have a custom template that can be returned for keys that are missing from the given context rather than an empty string. This really helps me identify visually where certain keys in my context are missing, and which keys those are. Is this possible?

@wycats
Copy link
Collaborator

wycats commented Dec 24, 2012

Yep! Just override the helperMissing helper and you're good to go!

This is the default:

Handlebars.registerHelper('helperMissing', function(arg) {
  if(arguments.length === 2) {
    return undefined;
  } else {
    throw new Error("Could not find property '" + arg + "'");
  }
});

Do this:

Handlebars.registerHelper('helperMissing', function(arg) {
  return "{{missing key - " + arg + "}}";
});

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

3 participants
@wycats @alindsay55661 and others