3

I try to write a Handlebar.Net helper which works like Equals. The Helper should be used like

{{#eq name "Foo"}}
    true
{{else}}
    false
{{/eq}}

But I don´t know how to implement this helper. In JS there is this example but I can´t find an example for C#.

My first shot was:

Handlebars.RegisterHelper("#eq", (output, context, data) =>
{
    if (data.Length != 2)
        output.WriteSafeString("false");

    output.WriteSafeString(data[0].Equals(data[1]));
});

But this just writes True or False into my file.

1

1 Answer 1

5

I´ve found the solution:

Handlebars.RegisterHelper(Equals, (output, options, context, data) => 
{
    if (data.Length != 2)
        options.Inverse(output, null);

    if (data[0].Equals(data[1]))
        options.Template(output, null);
    else
        options.Inverse(output, null);
});
Handlebars.RegisterHelper(LowerThan, (output, options, context, data) =>
{
    IntegerOperation(LowerThan, ref output, ref options, ref data);
});

Handlebars.RegisterHelper(GreaterThan, (output, options, context, data) =>
{
    IntegerOperation(GreaterThan, ref output, ref options, ref data);
});

Handlebars.RegisterHelper(LowerEquals, (output, options, context, data) =>
{
    IntegerOperation(LowerEquals, ref output, ref options, ref data);
});

Handlebars.RegisterHelper(GreaterEquals, (output, options, context, data) =>
{
    IntegerOperation(GreaterEquals, ref output, ref options, ref data);
});


private static void IntegerOperation(string operation, ref System.IO.TextWriter output, ref HelperOptions options, ref object[] data)
{
    if (data.Length != 2)
    {
        options.Inverse(output, null);
        return;
    }

    if (!int.TryParse(data[0].ToString(), out int leftValue))
    {
        options.Inverse(output, null);
        return;
    }

    if (!int.TryParse(data[1].ToString(), out int rightValue))
    {
        options.Inverse(output, null);
        return;
    }

    switch (operation)
    {
        case "lt":
            if (leftValue < rightValue)
                options.Template(output, null);
            else
                options.Inverse(output, null);
            break;
        case "le":
            if (leftValue <= rightValue)
                options.Template(output, null);
            else
                options.Inverse(output, null);
            break;
        case "gt":
            if (leftValue > rightValue)
                options.Template(output, null);
            else
                options.Inverse(output, null);
            break;
        case "ge":
            if (leftValue >= rightValue)
                options.Template(output, null);
            else
                options.Inverse(output, null);
            break;
        default:
            break;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.