{"record":{"id":"fa63db92fd39a0df","repo":"handlebars-lang/handlebars.js","slug":"if-requires-exactly-one-argument","errorCode":null,"errorMessage":"#if requires exactly one argument","messagePattern":"#if requires exactly one argument","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"lib/handlebars/helpers/if.js","lineNumber":7,"sourceCode":"import { Exception } from '@handlebars/parser';\nimport { isEmpty, isFunction } from '../utils.js';\n\nexport default function (instance) {\n  instance.registerHelper('if', function (conditional, options) {\n    if (arguments.length != 2) {\n      throw new Exception('#if requires exactly one argument');\n    }\n    if (isFunction(conditional)) {\n      conditional = conditional.call(this);\n    }\n\n    // Default behavior is to render the positive path if the value is truthy and not empty.\n    // The `includeZero` option may be set to treat the conditional as purely not empty based on the\n    // behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.\n    if ((!options.hash.includeZero && !conditional) || isEmpty(conditional)) {\n      return options.inverse(this);\n    } else {\n      return options.fn(this);\n    }\n  });\n\n  instance.registerHelper('unless', function (conditional, options) {\n    if (arguments.length != 2) {\n      throw new Exception('#unless requires exactly one argument');","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/handlebars-lang/handlebars.js/blob/13a7a679910d2adbfe7f6fad61ce8f98426b0378/lib/handlebars/helpers/if.js#L1-L25","documentation":"Handlebars' #if helper must receive exactly one conditional argument plus the compiled options object (arguments.length === 2). If the template supplies zero or more than one positional argument to #if, Handlebars throws this error at template-render time.","triggerScenarios":"{{#if}} with no argument (e.g. {{#if}}...{{/if}}); {{#if a b}} passing two positionals; calling instance.helpers.if(value) directly without options; a custom parser or whitespace/moustache syntax mistake merging arguments.","commonSituations":"Template typos after refactoring; concatenating template fragments so {{#if x}}{{/if}} loses its argument; passing extra positional args intending them as options — must use hash instead ({{#if x foo=bar}}); testing helpers directly without options.","solutions":["Ensure exactly one positional argument: {{#if value}}...{{/if}} — remove extras or move them to the hash: {{#if value key=val}}.","Add the missing argument if #if was written bare.","For multi-condition logic, combine in a registered helper or subexpression: {{#if (and a b)}} with an 'and' helper.","When calling the helper programmatically, pass both arguments: if(value, {fn, inverse, hash})."],"exampleFix":"// before\n{{#if user.isAdmin extraArg}}...{{/if}}\n// after\n{{#if user.isAdmin}}...{{/if}}","handlingStrategy":"validation","validationCode":"// Pre-render validation of template usage (static check example)\nconst bad = /\\{\\{#if\\s*(\\}|[^}]+\\s[^}\\s]+\\s+[^}\\s]+\\s*\\})/.test(source);\nif (bad) throw new Error('#if must have exactly one positional argument');","typeGuard":"function hasExactlyOneArg(conditional, options) {\n  return arguments.length === 2 && options && typeof options === 'object';\n}","tryCatchPattern":"try {\n  return template(data);\n} catch (e) {\n  if (e.message.includes('#if requires exactly one argument')) {\n    console.error('Template #if has zero or multiple args; fix the template expression');\n  }\n  throw e;\n}","preventionTips":["Always write {{#if value}} with exactly one positional argument","Pass extra data via hash syntax (key=value), not positionals","Combine complex conditions in registered helpers or subexpressions","Add template linting to catch bare or multi-arg #if","Test render all templates at build time to surface runtime errors early"],"tags":["handlebars","if-helper","wrong-argument-count"],"backgroundTag":"wrong-argument-count","analyzedSha":"13a7a679910d2adbfe7f6fad61ce8f98426b0378","analyzedAt":"2026-09-02T20:25:07.518Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}