admin管理员组

文章数量:1303530

I've been trying to modify the entity names for the classes generated by EFCore PowerTools using the handlebar templates but can't get the helpers to work. I thought DotNetHandleBars came with basic string helpers out of the box?

Has anyone got a guide on using the handlebar templates with EFCore PowerTools?

This is the edited template I'm using but I get the error listed below.

    {{> imports}}
 
namespace {{namespace}}
{
{{#if comment}}
    /// <summary>
    {{comment}}
    ///
</summary>
{{/if}}
{{#each class-annotations}}
{{{class-annotation}}}
{{/each}}
    public partial class {{[String-Replace] class "tbl"}} //test
    {
{{{> constructor}}}
{{{> properties}}}
    }
}

The error I get

HandlebarsDotNet.HandlebarsRuntimeException: Template references a helper that cannot be resolved. Helper '[String-Append]'

edit*

I've also tried adding the below to the top of the class.hbs file

var handlebarsContext = HandlebarsDotNet.Handlebars.Create();
HandlebarsHelpers.Register(handlebarsContext);

I've been trying to modify the entity names for the classes generated by EFCore PowerTools using the handlebar templates but can't get the helpers to work. I thought DotNetHandleBars came with basic string helpers out of the box?

Has anyone got a guide on using the handlebar templates with EFCore PowerTools?

This is the edited template I'm using but I get the error listed below.

    {{> imports}}
 
namespace {{namespace}}
{
{{#if comment}}
    /// <summary>
    {{comment}}
    ///
</summary>
{{/if}}
{{#each class-annotations}}
{{{class-annotation}}}
{{/each}}
    public partial class {{[String-Replace] class "tbl"}} //test
    {
{{{> constructor}}}
{{{> properties}}}
    }
}

The error I get

HandlebarsDotNet.HandlebarsRuntimeException: Template references a helper that cannot be resolved. Helper '[String-Append]'

https://github/ErikEJ/EFCorePowerTools

edit*

I've also tried adding the below to the top of the class.hbs file

var handlebarsContext = HandlebarsDotNet.Handlebars.Create();
HandlebarsHelpers.Register(handlebarsContext);
Share Improve this question edited Feb 5 at 10:03 Andrew asked Feb 4 at 17:02 AndrewAndrew 916 bronze badges 3
  • Did you read the documentation of HandleBars? Maybe you need to register one? – Odrai Commented Feb 4 at 19:43
  • Yes I have tried to register the helpers at the top of the class.hbs file but I don't know if this is the correct process for registering a helper? It didn't make any difference to the error I'm getting. – Andrew Commented Feb 5 at 10:05
  • Unlike JavaScript Handlebars, .NET’s HandlebarsDotNet doesn’t ship with string manipulation helpers. Also, String-Replace must be explicitly registered before Handlebars can recognize it. – AussieJoe Commented Feb 5 at 15:59
Add a comment  | 

1 Answer 1

Reset to default 0

According to your error

HandlebarsDotNet.HandlebarsRuntimeException: Template references a helper that cannot be resolved. Helper '[String-Append]'

You need to resolve your helper.

using HandlebarsDotNet;

public static class CustomHandlebarsHelpers
{
    public static void Register(IHandlebars hbs)
    {
        // String Replace Helper: [String-Replace] "tbl" will be removed
        hbs.RegisterHelper("String-Replace", (output, options, context, arguments) =>
        {
            if (arguments.Length == 2 && arguments[0] is string original && arguments[1] is string toRemove)
            {
                output.Write(original.Replace(toRemove, ""));
            }
        });
    }
}

You will also need to register the Helper in EFCore PowerTools:

var handlebars = Handlebars.Create();
CustomHandlebarsHelpers.Register(handlebars);

You will need to reference it correctly in your template:

{{> imports}}

namespace {{namespace}}
{
{{#if comment}}
    /// <summary>
    {{comment}}
    /// </summary>
{{/if}}
{{#each class-annotations}}
{{{class-annotation}}}
{{/each}}
    public partial class {{String-Replace class "tbl"}} // should work now!
    {
{{{> constructor}}}
{{{> properties}}}
    }
}

More specifically, you need to correct this line in your template:

public partial class {{String-Replace class "tbl"}} // should work now!
  • The square brackets [ ] are not needed in Handlebars.Net.
  • Handlebars helpers are registered without brackets.

本文标签: cEF core power tools and handlebars templateStack Overflow