tildeio / htmlbars

A variant of Handlebars that emits DOM and allows you to write helpers that manipulate live DOM nodes
MIT License
1.61k stars 193 forks source link

Implement new visitor #378

Closed mmun closed 9 years ago

mmun commented 9 years ago

Implements a new visitor that fully traverses the AST. It was designed in the spirit of Babel's transform. Usage:

import { traverse } from "htmlbars-syntax";

let ast = parse(`{{name-tag name=(upcase user.name)}}`);

traverse(ast, {
  PathExpression(node) {
    console.log(node.original); // Logs "name-tag", "upcase", "user.name"
  }
});

Alternatively, you can use the enter/exit format to capture when the node is entered and exited:

import { traverse } from "htmlbars-syntax";

let ast = parse(`...`);
let depth = -1;

traverse(ast, {
  Program: {
    enter() { depth++; },
    exit() { depth--; }
  }
});
mmun commented 9 years ago

There are more features coming for this visitor:

mixonic commented 9 years ago

Nice!!! :star: :star: :star: