Skip to content

How to treat 0 as truthy #285

Closed
Closed

Description

@sellomkantjwa

I tried using

testing Var: 
{{#if var includeZero=true}}
    var is set to {{var}}
{{/if}}

but I get

Unhandled Exception: System.InvalidOperationException: Sequence contains more than one element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.ConditionalBlockAccumulatorContext..ctor(Expression startingNode)
   at HandlebarsDotNet.Compiler.BlockAccumulatorContext.Create(Expression item, HandlebarsConfiguration configuration)
   at HandlebarsDotNet.Compiler.BlockAccumulator.ConvertTokens(IEnumerable`1 sequence)+MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.ExpressionBuilder.ConvertTokensToExpressions(IEnumerable`1 tokens)
   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(TextReader source)
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.Compile(String template)

Activity

  1. rexm commented on Feb 18, 2019

    @rexm
    Member

    Handlebars doesn’t support arbitrary statements.

    You could try an inverted #if helper like #not to test the value of var negatively.

  2. sellomkantjwa commented on Feb 18, 2019

    @sellomkantjwa
    Author

    Cool thanks,
    Turns out this syntax applies to the JS parser:
    handlebars-lang/handlebars.js#608

  3. tyriker commented on Jan 21, 2025

    @tyriker

    @rexm do you mean includeZero=true as being an arbitrary statement? The includeZero option has been a part of the #if helper since roughly 2014. https://handlebarsjs.com/guide/builtin-helpers.html#if

    Should Handlebars.NET support this option? I expected it to work and saw the same result.

    I believe the exception comes from here

    var argument = HandlebarsExpression.Boolish(helperExpression.Arguments.Single());
    -- which is just the reason for the Exception, not the underlying problem/solution.

    I have forked the repo to take a closer look, but can't promise anything and may opt for a workaround as it relates to my current needs.

  4. rexm commented on Jan 21, 2025

    @rexm
    Member

    I think I misunderstood what was originally being asked. I don’t think it would be terribly hard to extend the parser to handle this grammar.

  5. reopened this on Jan 21, 2025
  6. added a commit that references this issue on Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions