Module: Gitlab::TemplateParser::AST

Defined in:


AST nodes to evaluate when rendering a template.

Evaluating an AST is done by walking over the nodes and calling `evaluate`. This method takes two arguments:

  1. An instance of `EvalState`, used for tracking data such as the number of nested loops.

  2. An object used as the data for the current scope. This can be an Array, Hash, String, or something else. It's up to the AST node to determine what to do with it.

While tree walking interpreters (such as implemented here) aren't usually the fastest type of interpreter, they are:

  1. Fast enough for our use case

  2. Easy to implement and maintain

In addition, our AST interpreter doesn't allow for arbitrary code execution, unlike existing template engines such as Mustache ( or ERB.

Our interpreter also takes care of limiting the number of nested loops. And unlike Liquid, our interpreter is much smaller and thus has a smaller attack surface. Liquid isn't without its share of issues, such as

We also evaluated using Handlebars using the project Sadly, this implementation of Handlebars doesn't support control of whitespace (, and the project didn't appear to be maintained that much.

This doesn't mean these template engines aren't good, instead it means they won't work for our use case. For more information, refer to the comment

Defined Under Namespace

Classes: Each, Expressions, Identifier, If, Integer, Selector, Text, Transformer, Variable