首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Handlebars.Net If条件帮助器

Handlebars.Net If条件帮助器
EN

Stack Overflow用户
提问于 2019-12-10 22:12:09
回答 1查看 985关注 0票数 2

我试着写一个Handlebar.Net帮助器,它的工作方式与Equals相同。Helper应该像这样使用

代码语言:javascript
复制
{{#eq name "Foo"}}
    true
{{else}}
    false
{{/eq}}

但是我不知道如何实现这个帮助器。在JS中有this示例,但我找不到C#的示例。

我的第一个镜头是:

代码语言:javascript
复制
Handlebars.RegisterHelper("#eq", (output, context, data) =>
{
    if (data.Length != 2)
        output.WriteSafeString("false");

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

但这只是将True或False写入到我的文件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-11 18:27:20

我已经找到了解决方案:

代码语言:javascript
复制
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;
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59269178

复制
相关文章

相似问题

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