簡體   English   中英

把手無法解決助手中的綁定問題

[英]Handlebars not resolve binding within helper

我在handlebars.Net 上玩了一會兒,但BlockHelper 和綁定有問題。

模板(見下文)應該在人與人之間進行迭代,如果年齡超過 18 歲,則只將人寫入結果中。我認為我的助手工作正常,但 Handlebars.Net 無法解析我在 Helper-Block 中的綁定。

我有這個模板:

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

這是我的數據:

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

結果應該是這樣的:

Test
Luka Datrik
Test
John Doe

但是 Handlebars 不能解決我在 GreaterThan-Block 中的綁定問題 這是需要的還是錯誤?

這是我在 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");
        }

幫手:

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

        }
    }

我找到了解決方案:

等號示例:

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

我認為這是來自 options.Template(output, context) 的遞歸調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM