handlebars-lang/handlebars.js · error · Exception

#with requires exactly one argument

Error message

#with requires exactly one argument

What it means

Handlebars' built-in `with` block helper switches the evaluation context to its single argument and renders the block with it, or renders the inverse ({{else}}) section if the value is empty. The helper throws this Exception when it is invoked with anything other than exactly one argument plus the options object (arguments.length != 2). This guards against malformed template usage such as `{{with}}` with no argument or extra stray arguments.

Source

Thrown at lib/handlebars/helpers/with.js:7

import { Exception } from '@handlebars/parser';
import { isEmpty, isFunction } from '../utils.js';

export default function (instance) {
  instance.registerHelper('with', function (context, options) {
    if (arguments.length != 2) {
      throw new Exception('#with requires exactly one argument');
    }
    if (isFunction(context)) {
      context = context.call(this);
    }

    let fn = options.fn;

    if (!isEmpty(context)) {
      let data = options.data;

      return fn(context, {
        data: data,
        blockParams: [context],
      });
    } else {
      return options.inverse(this);
    }
  });

View on GitHub (pinned to 13a7a67991)

Solutions

  1. Fix the template so `{{with}}` receives exactly one argument: `{{with user}}...{{/with}}`
  2. Check for empty interpolated values or typos that swallowed/added arguments in the `{{with}}` expression
  3. If invoking the helper programmatically, pass exactly (context, options)
  4. Lint templates in CI to catch the malformed helper usage before runtime

Example fix

// before (template)
{{with}}
  {{name}}
{{/with}}

// after (template)
{{with user}}
  {{name}}
{{/with}}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate the with-helper argument before rendering
const expr = extractHelperExpression(templateSource, 'with'); // your own scanner
if (!expr || expr.args.length !== 1) {
  throw new Error(`{{with}} at ${expr?.loc} must have exactly one argument`);
}
render(templateSource, data);

Type guard

function hasExactlyOneWithArg(expr) {
  return Array.isArray(expr?.args) && expr.args.length === 1;
}

Try / catch

try {
  html = Handlebars.compile(tpl)(data);
} catch (e) {
  if (e.message.includes('#with requires exactly one argument')) {
    throw new Error(`Malformed {{with}} in template ${tplPath}: ${e.message}`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling the helper with `arguments.length != 2`, i.e. a template renders `{{with}}` with zero arguments, or `{{with a b}}` with more than one argument, or the helper is invoked programmatically with the wrong arity.

Common situations: A template was written or edited and the `{{with ...}}` expression lost its argument (e.g. `{{with}}...{{/with}}`); a dynamic partial or variable expansion produced an empty/extra token inside the `{{with}}` expression; a custom code path calls the registered helper directly with the wrong number of arguments.

Related errors


AI-assisted analysis of handlebars-lang/handlebars.js@13a7a67991 (2026-09-02). Data as JSON: /api/errors/e1b8ea68727d0151. Report an issue: GitHub.