Skip to content

Helper not registered first time #327

Closed
dotnet/arcade
#16884

Description

@joamla96

I've implemented a fairly simple helper to handle if conditions.
For some reason, when i run the code first time, it ends up throwing an exception that this specific helper is not registered. If I hit the endpoint again, it works perfectly fine and continues to work fine until I restart the application, where it will then throw the exception again the first time.

Don't think it'll help, but cant hurt, the exception it throws:

HandlebarsDotNet.HandlebarsRuntimeException: Template references a helper that is not registered. Could not find helper 'ifCond'
   at HandlebarsDotNet.Compiler.HelperFunctionBinder.LateBindHelperExpression(BindingContext context, String helperName, IEnumerable`1 arguments)
   at lambda_method(Closure , TextWriter , Object )
   at HandlebarsDotNet.Compiler.DeferredSectionVisitor.RenderSection(Object value, BindingContext context, Action`2 body, Action`2 inversion)
   at lambda_method(Closure , TextWriter , Object )
   at HandlebarsDotNet.Handlebars.HandlebarsEnvironment.<>c__DisplayClass7_0.<Compile>b__0(Object context)
   at FileGeneratorService.Service.GeneratePDF(VM_Quote quote) in D:\Users\jola\Source\Repos\Utility Portal\FileGeneratorService\Service.cs:line 224
   at WebAPI.Controllers.QuotesController.GetExport(Int32 id, String type) in D:\Users\jola\Source\Repos\Utility Portal\WebAPI\Controllers\QuotesController.cs:line 293
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at WebAPI.MiddlewareDeveloperException.Invoke(HttpContext context) in D:\Users\jola\Source\Repos\Utility Portal\WebAPI\MiddlewareDeveloperException.cs:line 23

and the helper itself:

Handlebars.RegisterHelper("ifCond", (writer, options, context, arguments) =>
{
	if (arguments[0] == null || arguments[1] == null)
	{
		options.Inverse(writer, (object)context);
		return;
	}

	if (arguments[0].ToString() == arguments[1].ToString())
	{
		options.Template(writer, (object)context);
	} else {
		options.Inverse(writer, (object)context);
	}
});

Activity

  1. rexm commented on Dec 11, 2019

    @rexm
    Member

    Can you please also share the template and the code where it is being compiled and invoked?

  2. joamla96 commented on Jan 7, 2020

    @joamla96
    Author

    A template where its being used looks like this:

    		<!-- Spacing Element, because floats are weird -->
    		<div style="clear: both; padding-top: 15px;">&nbsp;</div>
    
            {{Quote.FreeText}}
    
    		{{#productGroups}}
    		{{ifCond Category.SystemType 'eq' 'iaas'}}
    		{{> ProductGroupsIaas}}
    		{{else}}
    		{{> ProductGroups}}
    		{{/ifCond}}
    		{{/productGroups}}
    
    		<br /><br />
    
    		<table>
    			<thead>
    

    and the invocation is set up like this:

    var bodyTask = File.ReadAllTextAsync(path + "/Body.html");
    var headerTask = File.ReadAllTextAsync(path + "/Header.html");
    var footerTask = File.ReadAllTextAsync(path + "/Footer.html");
    
    var bodyTemplate = Handlebars.Compile(await bodyTask);
    var headerTemplate = Handlebars.Compile(await headerTask);
    var footerTemplate = Handlebars.Compile(await footerTask);
    
    Handlebars.RegisterHelper("ifCond", (writer, options, context, arguments) =>
                {
                    if (arguments[0] == null || arguments[1] == null || arguments[2] == null)
                    {
                        options.Inverse(writer, (object)context);
                        return;
                    }
    
                    var passed = false;
    
                    switch (arguments[1])
                    {
                        case "eq":
                            if (arguments[0].ToString() == arguments[2].ToString()) passed = true;
                            break;
    
                        case "grt":
                            if (decimal.Parse(arguments[0].ToString()) > decimal.Parse(arguments[2].ToString())) passed = true;
                            break;
                    }
    
    
                    if (passed)
                    {
                        options.Template(writer, (object)context);
                    }
                    else
                    {
                        options.Inverse(writer, (object)context);
                    }
                });
    var data = new BodyData();
    data.Quote = quote;
    
    var dataHF = new DataHeaderFooter();
    dataHF.Quote = quote;
    dataHF.LogoPath = this.config["LogoPath"];
    
    var bodyHtml = bodyTemplate(data);
    var headerHtml = headerTemplate(dataHF);
    var footerHtml = footerTemplate(dataHF);
    
    var render = new HtmlToPdf();
    
    var header = new HtmlHeaderFooter() { HtmlFragment = headerHtml, LoadStylesAndCSSFromMainHtmlDocument = true, Height = 30, DrawDividerLine = false };
    var footer = new HtmlHeaderFooter() { HtmlFragment = footerHtml, LoadStylesAndCSSFromMainHtmlDocument = true };
    
    render.PrintOptions.MarginTop = header.Height;
    render.PrintOptions.MarginBottom = footer.Height;
    render.PrintOptions.MarginLeft = 0;
    render.PrintOptions.MarginRight = 0;
    
    var result = render
    	.RenderHtmlAsPdf(bodyHtml)
    	.AddHTMLHeaders(header)
    	.AddHTMLFooters(footer);
    
    return result.Stream;
    

    Sorry for the late reply, it got drowned in my feed :D

  3. artemutin commented on May 7, 2020

    @artemutin

    I had kind of similar issue, and I was able to fix it by moving helper registration before compilation of any templates

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