Skip to content

Conversation

@TheConstructor
Copy link

I authored a partial-template with optional block-includes and happened to write {{#>mail}}{{/mail}} for the test-mail, as I did not need to adjust the optionals. This led to an exception similar to the one below:

System.InvalidOperationException
Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.PartialBlockAccumulatorContext.GetAccumulatedBlock() in ..\Handlebars.Net\source\Handlebars\Compiler\Lexer\Converter\BlockAccumulators\PartialBlockAccumulatorContext.cs:line 27
   at HandlebarsDotNet.Compiler.BlockAccumulator.AccumulateBlock(Expression parentItem, IEnumerator`1 enumerator, BlockAccumulatorContext context) in ..\Handlebars.Net\source\Handlebars\Compiler\Lexer\Converter\BlockAccumulator.cs:line 56
   at HandlebarsDotNet.Compiler.BlockAccumulator.ConvertTokens(IEnumerable`1 sequence)+MoveNext() in ..\Handlebars.Net\source\Handlebars\Compiler\Lexer\Converter\BlockAccumulator.cs:line 32
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at HandlebarsDotNet.Compiler.BlockAccumulator.Accumulate(IEnumerable`1 tokens, ICompiledHandlebarsConfiguration configuration) in ..\Handlebars.Net\source\Handlebars\Compiler\Lexer\Converter\BlockAccumulator.cs:line 13
   at HandlebarsDotNet.Compiler.ExpressionBuilder.ConvertTokensToExpressions(IEnumerable`1 tokens, ICompiledHandlebarsConfiguration configuration) in ..\Handlebars.Net\source\Handlebars\Compiler\ExpressionBuilder.cs:line 25
   at HandlebarsDotNet.Compiler.HandlebarsCompiler.Compile(ExtendedStringReader source, CompilationContext compilationContext) in ..\Handlebars.Net\source\Handlebars\Compiler\HandlebarsCompiler.cs:line 25
   at HandlebarsDotNet.HandlebarsEnvironment.Compile(TextReader template) in ..\Handlebars.Net\source\Handlebars\HandlebarsEnvironment.cs:line 145
   at HandlebarsDotNet.HandlebarsEnvironment.Compile(String template) in ..\Handlebars.Net\source\Handlebars\HandlebarsEnvironment.cs:line 188
   at HandlebarsDotNet.Handlebars.Compile(String template) in ..\Handlebars.Net\source\Handlebars\Handlebars.cs:line 63
   at HandlebarsDotNet.Test.InlinePartialTests.BasicBlockInlinePartial() in ..

Being new to Handlebars.Net it took me a while to understand what really was wrong. I no know, that I can change the template to {{>mail}}, but the exception isn't really helpful and I don't know why it shouldn't just render. So this pull-request allows block-includes with empty blocks.

On a side-note {{#>mail}}{{/mail}} renders, if no mail-partial is present, while {{>mail}} throws an exception, if no MissingPartialTemplateHandler is registered. While not necessary for my use-case it may even be nice to have a syntax, that allows for optional partials out-of-the-box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant