Handlebars - the next generation
Language specification

Helpers

The user can register helpers with the Handlebars runtime A mustache-statement with a single id is interpreted as helper call.

07-helpers/helper-without-args.hb-spec.json
A helper without args
Template:
{{help}}
Input / Output
Input
{}
Helpers
help
=
Returns the literal string 'a'
Output
a
AST
{
  "type": "Program",
  "body": [
    {
      "type": "MustacheStatement",
      "escaped": true,
      "params": [],
      "path": {
        "type": "PathExpression",
        "original": "help",
        "data": false,
        "depth": 0,
        "parts": ["help"],
        "loc": {
          "start": { "line": 1, "column": 2 },
          "end": { "line": 1, "column": 6 }
        }
      },
      "strip": { "open": false, "close": false },
      "loc": {
        "start": { "line": 1, "column": 0 },
        "end": { "line": 1, "column": 8 }
      }
    }
  ],
  "strip": {},
  "loc": {
    "start": { "line": 1, "column": 0 },
    "end": { "line": 1, "column": 8 }
  }
}

If a helper-name and an input property have the same value, the helper is chosen.

07-helpers/helpers-have-precedence-over-input-props.hb-spec.json
Helpers have precedence over input properties
Template:
{{help}}
Input / Output
Input
{ "help": "b" }
Helpers
help
=
Returns the literal string 'a'
Output
a
AST
{
  "type": "Program",
  "body": [
    {
      "type": "MustacheStatement",
      "escaped": true,
      "params": [],
      "path": {
        "type": "PathExpression",
        "original": "help",
        "data": false,
        "depth": 0,
        "parts": ["help"],
        "loc": {
          "start": { "line": 1, "column": 2 },
          "end": { "line": 1, "column": 6 }
        }
      },
      "strip": { "open": false, "close": false },
      "loc": {
        "start": { "line": 1, "column": 0 },
        "end": { "line": 1, "column": 8 }
      }
    }
  ],
  "strip": {},
  "loc": {
    "start": { "line": 1, "column": 0 },
    "end": { "line": 1, "column": 8 }
  }
}

Helper parameters

A mustache can be a helper call with space-separated parameters. Each parameter is a PathExpression or a Literal expression

HelperParameters
MustacheParameters ::
Whitespace MustacheParameter MustacheParametersopt
MustacheParameter ::
Literal
PathExpression
SubExpression

07-helpers/helper-with-args.hb-spec.json
A helper with string-args
Template:
{{help first second}}
Input / Output
Input
{ "first": "hello", "second": "world" }
Helpers
help
=
Concatenates two strings
Output
helloworld
AST
{
  "type": "Program",
  "body": [
    {
      "type": "MustacheStatement",
      "escaped": true,
      "params": [
        {
          "type": "PathExpression",
          "original": "first",
          "data": false,
          "depth": 0,
          "parts": ["first"],
          "loc": {
            "start": { "line": 1, "column": 7 },
            "end": { "line": 1, "column": 12 }
          }
        },
        {
          "type": "PathExpression",
          "original": "second",
          "data": false,
          "depth": 0,
          "parts": ["second"],
          "loc": {
            "start": { "line": 1, "column": 13 },
            "end": { "line": 1, "column": 19 }
          }
        }
      ],
      "path": {
        "type": "PathExpression",
        "original": "help",
        "data": false,
        "depth": 0,
        "parts": ["help"],
        "loc": {
          "start": { "line": 1, "column": 2 },
          "end": { "line": 1, "column": 6 }
        }
      },
      "strip": { "open": false, "close": false },
      "loc": {
        "start": { "line": 1, "column": 0 },
        "end": { "line": 1, "column": 21 }
      }
    }
  ],
  "strip": {},
  "loc": {
    "start": { "line": 1, "column": 0 },
    "end": { "line": 1, "column": 21 }
  }
}