{"record":{"id":"e1b8ea68727d0151","repo":"handlebars-lang/handlebars.js","slug":"with-requires-exactly-one-argument","errorCode":null,"errorMessage":"#with requires exactly one argument","messagePattern":"#with requires exactly one argument","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"lib/handlebars/helpers/with.js","lineNumber":7,"sourceCode":"import { Exception } from '@handlebars/parser';\nimport { isEmpty, isFunction } from '../utils.js';\n\nexport default function (instance) {\n  instance.registerHelper('with', function (context, options) {\n    if (arguments.length != 2) {\n      throw new Exception('#with requires exactly one argument');\n    }\n    if (isFunction(context)) {\n      context = context.call(this);\n    }\n\n    let fn = options.fn;\n\n    if (!isEmpty(context)) {\n      let data = options.data;\n\n      return fn(context, {\n        data: data,\n        blockParams: [context],\n      });\n    } else {\n      return options.inverse(this);\n    }\n  });","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/handlebars-lang/handlebars.js/blob/13a7a679910d2adbfe7f6fad61ce8f98426b0378/lib/handlebars/helpers/with.js#L1-L25","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the template so `{{with}}` receives exactly one argument: `{{with user}}...{{/with}}`","Check for empty interpolated values or typos that swallowed/added arguments in the `{{with}}` expression","If invoking the helper programmatically, pass exactly (context, options)","Lint templates in CI to catch the malformed helper usage before runtime"],"exampleFix":"// before (template)\n{{with}}\n  {{name}}\n{{/with}}\n\n// after (template)\n{{with user}}\n  {{name}}\n{{/with}}","handlingStrategy":"validation","validationCode":"// Pre-validate the with-helper argument before rendering\nconst expr = extractHelperExpression(templateSource, 'with'); // your own scanner\nif (!expr || expr.args.length !== 1) {\n  throw new Error(`{{with}} at ${expr?.loc} must have exactly one argument`);\n}\nrender(templateSource, data);","typeGuard":"function hasExactlyOneWithArg(expr) {\n  return Array.isArray(expr?.args) && expr.args.length === 1;\n}","tryCatchPattern":"try {\n  html = Handlebars.compile(tpl)(data);\n} catch (e) {\n  if (e.message.includes('#with requires exactly one argument')) {\n    throw new Error(`Malformed {{with}} in template ${tplPath}: ${e.message}`);\n  }\n  throw e;\n}","preventionTips":["Always write {{with value}} with exactly one expression","Lint templates (e.g. with a Handlebars linter) in CI to catch argument-less block helpers","Escape or strip user-supplied fragments before embedding them into templates so they cannot alter helper arity","Add a render-time test per template to catch malformed helper usage early"],"tags":["handlebars","template","helper-arity","with-helper"],"backgroundTag":"helper-arity-mismatch","analyzedSha":"13a7a679910d2adbfe7f6fad61ce8f98426b0378","analyzedAt":"2026-09-02T20:25:07.518Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}