Skip to content

Commit

Permalink
Fix compiler-api example (#1303)
Browse files Browse the repository at this point in the history
Closes #1302 

While trying to answer #1302, I noticed that the compiler-api example did not work in (because `nameLookup` is actually at the prototype). This example has a jsfiddle that proves it is working.
It does not provide an solve the exact problem of the reporter, but it answers the question.
  • Loading branch information
nknapp authored Jan 29, 2017
1 parent 6300e57 commit c997020
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions docs/compiler-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,36 @@ The `Handlebars.JavaScriptCompiler` object has a number of methods that may be c
- `initializeBuffer()`
Allows for buffers other than the default string buffer to be used. Generally needs to be paired with a custom `appendToBuffer` implementation.

### Example for the compiler api.

This example changes all lookups of properties are performed by a helper (`lookupLowerCase`) which looks for `test` if `{{Test}}` occurs in the template. This is just to illustrate how compiler behavior can be change.

There is also [a jsfiddle with this code](https://jsfiddle.net/9D88g/85/) if you want to play around with it.


```javascript
function MyCompiler() {
Handlebars.JavaScriptCompiler.apply(this, arguments);
}
MyCompiler.prototype = Object.create(Handlebars.JavaScriptCompiler);
MyCompiler.prototype = new Handlebars.JavaScriptCompiler();

MyCompiler.nameLookup = function(parent, name, type) {
if (type === 'partial') {
return 'MyPartialList[' + JSON.stringify(name) ']';
MyCompiler.prototype.nameLookup = function(parent, name, type) {
if (type === 'context') {
return this.source.functionCall('helpers.lookupLowerCase', '', [parent, JSON.stringify(name)])
} else {
return Handlebars.JavaScriptCompiler.prototype.nameLookup.call(this, parent, name, type);
}
};
}

var env = Handlebars.create();
env.registerHelper('lookupLowerCase', function(parent, name) {
return parent[name.toLowerCase()]
})

env.JavaScriptCompiler = MyCompiler;
env.compile('my template');

var template = env.compile('{{#each Test}}{{.}} {{/each}}');
console.log(template({
test: [ "a","b","c"]
}));
```

4 comments on commit c997020

@bdiz
Copy link

@bdiz bdiz commented on c997020 Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nknapp thanks for this example. When using the with helper, the lookupLowerCase helper no longer gets called. I've tried a few things without success to get this working. I think the with helper is being executed with the original Handlebars. Can you update the example if you know how to get this working? Thanks.

@nknapp
Copy link
Collaborator Author

@nknapp nknapp commented on c997020 Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, could you create a separate issue with your question? It would also be helpful if you create a fiddle based on https://jsfiddle.net/9D88g/85/ to show what you have tried. As far as can tell, it works with the with-helper https://jsfiddle.net/9D88g/160/

@bdiz
Copy link

@bdiz bdiz commented on c997020 Aug 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll create a new issue. Here's a fiddle demonstrating what I'm talking about: https://jsfiddle.net/ujp6v98h/ I'll put it in the issue. Thanks.

@bdiz
Copy link

@bdiz bdiz commented on c997020 Aug 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created: #1376

Please sign in to comment.