首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Handlebar未解析帮助器中的绑定

Handlebar未解析帮助器中的绑定
EN

Stack Overflow用户
提问于 2019-12-12 23:08:03
回答 1查看 303关注 0票数 1

我用过一点handlebars.Net,在使用BlockHelper和绑定时遇到了问题。

模板(见下文)应该在persons之间迭代,并且只在年龄超过18岁时才将persons写入结果。我认为我的helper工作得很好,但Handlebars.Net不能解析Helper-Block中的绑定。

我有这个模板:

代码语言:javascript
复制
{{#Persons}}
    {{gt Age '18'}}
        Test
        {{Firstname}} {{Lastname}}
    {{/gt}}
{{/Persons}}

这是我的数据:

代码语言:javascript
复制
                {
                    new
                    {
                        Firstname = "Luka",
                        Lastname = "Datrik",
                        Age = "28"
                    },
                    new
                    {
                        Firstname = "Max",
                        Lastname = "Mustermann",
                        Age = "18"
                    },
                    new
                    {
                        Firstname = "John",
                        Lastname = "Doe",
                        Age = "33"
                    }
                };

结果应该是这样的:

代码语言:javascript
复制
Test
Luka Datrik
Test
John Doe

但是Handlebar不能解决我在GreaterThan Block中的绑定-这是想要的还是一个bug?

下面是我用C#编写的完整代码

代码语言:javascript
复制
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");
        }

帮助者:

代码语言:javascript
复制
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;
            }

        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-13 17:50:08

我已经找到了解决方案:

Equals示例:

代码语言:javascript
复制
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(输出,上下文)的递归调用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59307560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档