简体   繁体   English

把手无法解决助手中的绑定问题

[英]Handlebars not resolve binding within helper

I play a little with handlebars.Net and have a problem with BlockHelper and bindings.我在handlebars.Net 上玩了一会儿,但BlockHelper 和绑定有问题。

The template (see below) should iterate between persons and only write persons into the result if it´s age is over 18. I think my helper works fine but Handlebars.Net doesn't resolve my binding within the Helper-Block.模板(见下文)应该在人与人之间进行迭代,如果年龄超过 18 岁,则只将人写入结果中。我认为我的助手工作正常,但 Handlebars.Net 无法解析我在 Helper-Block 中的绑定。

I've got this template:我有这个模板:

{{#Persons}}
    {{gt Age '18'}}
        Test
        {{Firstname}} {{Lastname}}
    {{/gt}}
{{/Persons}}

This is my data:这是我的数据:

                {
                    new
                    {
                        Firstname = "Luka",
                        Lastname = "Datrik",
                        Age = "28"
                    },
                    new
                    {
                        Firstname = "Max",
                        Lastname = "Mustermann",
                        Age = "18"
                    },
                    new
                    {
                        Firstname = "John",
                        Lastname = "Doe",
                        Age = "33"
                    }
                };

The result should be something like:结果应该是这样的:

Test
Luka Datrik
Test
John Doe

But Handlebars doesn´t resolve my binding within the GreaterThan-Block Is this wanted or a bug?但是 Handlebars 不能解决我在 GreaterThan-Block 中的绑定问题 这是需要的还是错误?

Here my complete code in C#这是我在 C# 中的完整代码

static void Main(string[] args)
        {
            HandlebarsHelper.Register();

            var rawFile = File.ReadAllText(".\\TextFile1.txt");

            //var content = new XDocument(rawFile).Element("PrintLayout").FirstNode.ToString();

            var template = Handlebars.Compile(rawFile);
            var result = template(new Dynamic());

            File.WriteAllText("temp.txt", result);

            System.Diagnostics.Process.Start("temp.txt");
        }

Helper:帮手:

namespace HandlebarsTests
{
    public static class HandlebarsHelper
    {
        public static readonly new string Equals = "eq";
        public static readonly string LowerThan = "lt";
        public static readonly string GreaterThan = "gt";
        public static readonly string GreaterEquals = "ge";
        public static readonly string LowerEquals = "le";
        public static readonly string DateFormat = "dateFormat";
        public static readonly string FormatStringIndicator = "formatString";

        public static void Register()
        {
            Handlebars.RegisterHelper(DateFormat, (output, context, data) =>
            {
                if (!DateTime.TryParse(data[0].ToString(), out DateTime date))
                {
                    output.WriteSafeString(data[0].ToString());
                    return;
                }

                var dictionary = data[1] as HandlebarsDotNet.Compiler.HashParameterDictionary;
                var formatString = dictionary[FormatStringIndicator];

                output.WriteSafeString(date.ToString(formatString.ToString()));
            });

            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;
            }

        }
    }

I´ve found the solution:我找到了解决方案:

Example for Equals:等号示例:

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

                if (data[0].Equals(data[1]))
                    options.Template(output, context);
                else
                    options.Inverse(output, context);
            });

I think it´sa recursive call from options.Template(output, context)我认为这是来自 options.Template(output, context) 的递归调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM