1 # Handlebars Compiler APIs
3 There are a number of formal APIs that tool implementors may interact with.
7 Other tools may interact with the formal AST as defined below. Any JSON structure matching this pattern may be used and passed into the `compile` and `precompile` methods in the same way as the text for a template.
9 AST structures may be generated either with the `Handlebars.parse` method and then manipulated, via the `Handlebars.AST` objects of the same name, or constructed manually as a generic JavaScript object matching the structure defined below.
12 var ast = Handlebars.parse(myTemplate);
16 Handlebars.precompile(ast);
25 loc: SourceLocation | null;
28 interface SourceLocation {
29 source: string | null;
43 interface Program <: Node {
47 blockParams: [ string ];
54 interface Statement <: Node { }
56 interface MustacheStatement <: Statement {
57 type: "MustacheStatement";
59 path: PathExpression | Literal;
60 params: [ Expression ];
64 strip: StripFlags | null;
67 interface BlockStatement <: Statement {
68 type: "BlockStatement";
70 params: [ Expression ];
73 program: Program | null;
74 inverse: Program | null;
76 openStrip: StripFlags | null;
77 inverseStrip: StripFlags | null;
78 closeStrip: StripFlags | null;
81 interface PartialStatement <: Statement {
82 type: "PartialStatement";
83 name: PathExpression | SubExpression;
84 params: [ Expression ];
88 strip: StripFlags | null;
91 interface PartialBlockStatement <: Statement {
92 type: "PartialBlockStatement";
93 name: PathExpression | SubExpression;
94 params: [ Expression ];
97 program: Program | null;
100 openStrip: StripFlags | null;
101 closeStrip: StripFlags | null;
105 `name` will be a `SubExpression` when tied to a dynamic partial, i.e. `{{> (foo) }}`, otherwise this is a path or literal whose `original` value is used to lookup the desired partial.
109 interface ContentStatement <: Statement {
110 type: "ContentStatement";
115 interface CommentStatement <: Statement {
116 type: "CommentStatement";
119 strip: StripFlags | null;
125 interface Decorator <: Statement {
128 path: PathExpression | Literal;
129 params: [ Expression ];
132 strip: StripFlags | null;
135 interface DecoratorBlock <: Statement {
136 type: "DecoratorBlock";
137 path: PathExpression | Literal;
138 params: [ Expression ];
141 program: Program | null;
143 openStrip: StripFlags | null;
144 closeStrip: StripFlags | null;
148 Decorator paths only utilize the `path.original` value and as a consequence do not support depthed evaluation.
153 interface Expression <: Node { }
159 interface SubExpression <: Expression {
160 type: "SubExpression";
161 path: PathExpression;
162 params: [ Expression ];
170 interface PathExpression <: Expression {
171 type: "PathExpression";
179 - `data` is true when the given expression is a `@data` reference.
180 - `depth` is an integer representation of which context the expression references. `0` represents the current context, `1` would be `../`, etc.
181 - `parts` is an array of the names in the path. `foo.bar` would be `['foo', 'bar']`. Scope references, `.`, `..`, and `this` should be omitted from this array.
182 - `original` is the path as entered by the user. Separator and scope references are left untouched.
188 interface Literal <: Expression { }
190 interface StringLiteral <: Literal {
191 type: "StringLiteral";
196 interface BooleanLiteral <: Literal {
197 type: "BooleanLiteral";
202 interface NumberLiteral <: Literal {
203 type: "NumberLiteral";
208 interface UndefinedLiteral <: Literal {
209 type: "UndefinedLiteral";
212 interface NullLiteral <: Literal {
221 interface Hash <: Node {
226 interface HashPair <: Node {
232 interface StripFlags {
238 `StripFlags` are used to signify whitespace control character that may have been entered on a given statement.
242 `Handlebars.Visitor` is available as a base class for general interaction with AST structures. This will by default traverse the entire tree and individual methods may be overridden to provide specific responses to particular nodes.
244 Recording all referenced partial names:
247 var Visitor = Handlebars.Visitor;
249 function ImportScanner() {
252 ImportScanner.prototype = new Visitor();
254 ImportScanner.prototype.PartialStatement = function(partial) {
255 this.partials.push({request: partial.name.original});
257 Visitor.prototype.PartialStatement.call(this, partial);
260 var scanner = new ImportScanner();
264 The current node's ancestors will be maintained in the `parents` array, with the most recent parent listed first.
266 The visitor may also be configured to operate in mutation mode by setting the `mutation` field to true. When in this mode, handler methods may return any valid AST node and it will replace the one they are currently operating on. Returning `false` will remove the given value (if valid) and returning `undefined` will leave the node in tact. This return structure only apply to mutation mode and non-mutation mode visitors are free to return whatever values they wish.
268 Implementors that may need to support mutation mode are encouraged to utilize the `acceptKey`, `acceptRequired` and `acceptArray` helpers which provide the conditional overwrite behavior as well as implement sanity checks where pertinent.
270 ## JavaScript Compiler
272 The `Handlebars.JavaScriptCompiler` object has a number of methods that may be customized to alter the output of the compiler:
274 - `nameLookup(parent, name, type)`
275 Used to generate the code to resolve a give path component.
277 - `parent` is the existing code in the path resolution
278 - `name` is the current path component
279 - `type` is the type of name being evaluated. May be one of `context`, `data`, `helper`, `decorator`, or `partial`.
281 Note that this does not impact dynamic partials, which implementors need to be aware of. Overriding `VM.resolvePartial` may be required to support dynamic cases.
283 - `depthedLookup(name)`
284 Used to generate code that resolves parameters within any context in the stack. Is only used in `compat` mode.
287 Allows for custom compiler flags used in the runtime version checking logic.
289 - `appendToBuffer(source, location, explicit)`
290 Allows for code buffer emitting code. Defaults behavior is string concatenation.
292 - `source` is the source code whose result is to be appending
293 - `location` is the location of the source in the source map.
294 - `explicit` is a flag signaling that the emit operation must occur, vs. the lazy evaled options otherwise.
296 - `initializeBuffer()`
297 Allows for buffers other than the default string buffer to be used. Generally needs to be paired with a custom `appendToBuffer` implementation.
300 function MyCompiler() {
301 Handlebars.JavaScriptCompiler.apply(this, arguments);
303 MyCompiler.prototype = Object.create(Handlebars.JavaScriptCompiler);
305 MyCompiler.nameLookup = function(parent, name, type) {
306 if (type === 'partial') {
307 return 'MyPartialList[' + JSON.stringify(name) ']';
309 return Handlebars.JavaScriptCompiler.prototype.nameLookup.call(this, parent, name, type);
313 var env = Handlebars.create();
314 env.JavaScriptCompiler = MyCompiler;
315 env.compile('my template');