Skip to content

Commit c997020

Browse files
authored
Fix compiler-api example (#1303)
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.
1 parent 6300e57 commit c997020

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

docs/compiler-api.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,36 @@ The `Handlebars.JavaScriptCompiler` object has a number of methods that may be c
296296
- `initializeBuffer()`
297297
Allows for buffers other than the default string buffer to be used. Generally needs to be paired with a custom `appendToBuffer` implementation.
298298

299+
### Example for the compiler api.
300+
301+
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.
302+
303+
There is also [a jsfiddle with this code](https://jsfiddle.net/9D88g/85/) if you want to play around with it.
304+
305+
299306
```javascript
300307
function MyCompiler() {
301308
Handlebars.JavaScriptCompiler.apply(this, arguments);
302309
}
303-
MyCompiler.prototype = Object.create(Handlebars.JavaScriptCompiler);
310+
MyCompiler.prototype = new Handlebars.JavaScriptCompiler();
304311

305-
MyCompiler.nameLookup = function(parent, name, type) {
306-
if (type === 'partial') {
307-
return 'MyPartialList[' + JSON.stringify(name) ']';
312+
MyCompiler.prototype.nameLookup = function(parent, name, type) {
313+
if (type === 'context') {
314+
return this.source.functionCall('helpers.lookupLowerCase', '', [parent, JSON.stringify(name)])
308315
} else {
309316
return Handlebars.JavaScriptCompiler.prototype.nameLookup.call(this, parent, name, type);
310317
}
311-
};
318+
}
312319

313320
var env = Handlebars.create();
321+
env.registerHelper('lookupLowerCase', function(parent, name) {
322+
return parent[name.toLowerCase()]
323+
})
324+
314325
env.JavaScriptCompiler = MyCompiler;
315-
env.compile('my template');
326+
327+
var template = env.compile('{{#each Test}}{{.}} {{/each}}');
328+
console.log(template({
329+
test: [ "a","b","c"]
330+
}));
316331
```

0 commit comments

Comments
 (0)