车把 .NET 如果比较

本文关键字:比较 如果 NET 车把 | 更新日期: 2023-09-27 17:57:09

我使用Handlebars .NET作为我的邮件模板,所以我在MVC ASP.NET 服务器端宽度生成模板。我需要这样的比较。但它不起作用吗?我能做什么?

//Product.ProdType is a enum property 
{{#if (Product.ProdType=='BlaBlaBla')}}
<p>This is a test</p>
{{/if}}

车把 .NET 如果比较

我遇到了同样的问题,我创建了一个处理数字数据类型和字符串的辅助函数"ifCond"。它可以优化或扩展以使用其他类型的类型。

Handlebars.RegisterHelper("ifCond",
        (writer, context, args) =>
        {
            if (args.Length != 5)
            {
                writer.Write("ifCond:Wrong number of arguments");
                return;
            }
            if (args[0] == null || args[0].GetType().Name == "UndefinedBindingResult")
            {
                writer.Write("ifCond:args[0] undefined");
                return;
            }
            if (args[1] == null || args[1].GetType().Name == "UndefinedBindingResult")
            {
                writer.Write("ifCond:args[1] undefined");
                return;
            }
            if (args[2] == null || args[2].GetType().Name == "UndefinedBindingResult")
            {
                writer.Write("ifCond:args[2] undefined");
                return;
            }
            if (args[0].GetType().Name == "String")
            {
                string val1 = args[0].ToString();
                string val2 = args[2].ToString();
                switch (args[1].ToString())
                {
                    case ">":
                        writer.Write(val1.Length > val2.Length ? args[3] : args[4]);
                        break;
                    case "=":
                    case "==":
                        writer.Write(val1 == val2 ? args[3] : args[4]);
                        break;
                    case "<":
                        writer.Write(val1.Length < val2.Length ? args[3] : args[4]);
                        break;
                    case "!=":
                    case "<>":
                        writer.Write(val1 != val2 ? args[3] : args[4]);
                        break;
                }
            }
            else
            {
                float val1 = float.Parse(args[0].ToString());
                float val2 = float.Parse(args[2].ToString());
                switch (args[1].ToString())
                {
                    case ">":
                        writer.Write(val1 > val2 ? args[3] : args[4]);
                        break;
                    case "=":
                    case "==":
                        writer.Write(val1 == val2 ? args[3] : args[4]);
                        break;
                    case "<":
                        writer.Write(val1 < val2 ? args[3] : args[4]);
                        break;
                    case "<=":
                        writer.Write(val1 <= val2 ? args[3] : args[4]);
                        break;
                    case ">=":
                        writer.Write(val1 >= val2 ? args[3] : args[4]);
                        break;
                    case "!=":
                    case "<>":
                        writer.Write(val1 != val2 ? args[3] : args[4]);
                        break;
                }
            }

这里有一些单元测试解释了用法:

var template = Handlebars.Compile("{{{ifCond test '>' 1 '>1' '<=1' }}}");
var data = new { test = 2 };
var result = template(data);
Assert.AreEqual(">1", result);
data = new { test = 0 };
result = template(data);
Assert.AreEqual("<=1", result);
template = Handlebars.Compile("{{{ifCond test '=' 1 'eq' 'not eq' }}}");
data = new { test = 1 };
result = template(data);
Assert.AreEqual("eq", result);
data = new { test = 0 };
result = template(data);
Assert.AreEqual("not eq", result);
template = Handlebars.Compile("{{{ifCond test '!=' 1 'diff' 'eq' }}}");
data = new { test = 2 };
result = template(data);
Assert.AreEqual("diff", result);
template = Handlebars.Compile("{{{ifCond str '!=' '' 'not empty' 'empty' }}}");
var datastr = new { str = "abc" };
result = template(datastr);
Assert.AreEqual("not empty", result);
template = Handlebars.Compile("{{{ifCond str '==' '' 'empty' 'not empty' }}}");
datastr = new { str = "" };
result = template(datastr);
Assert.AreEqual("empty", result);
希望

它有所帮助,我也希望更好的实现,更优雅,我的解决方案有效,但我认为它可以以更简洁的方式完成。

下面是模板中具有 Int32 值的用法示例:

{{ifCond MycountVariable '>' 1 'more than one' 'one'}}

下面是模板中带有字符串值的使用示例:

{{ifCond MyStringVariable '!=' '' MyStringVariable 'empty value'}}

我正在使用 Handlebars.NET 作者在其单元测试中承诺的方式,它就像一个魅力:

Handlebars.RegisterHelper("ifCond", (writer, options, context, arguments) =>
{
    if (arguments[0] == arguments[1])
    {
        options.Template(writer, (object)context);
    }
    else
    {
        options.Inverse(writer, (object)context);
    }
});

(至少在 1.10.1 Handlebars.NET 中)。

像你试图做的逻辑必须在一个辅助函数中。您不能将这样的关系运算符直接放入车把模板中。它是故意这样设计的。助手非常易于创建和使用。有关详细信息 http://handlebarsjs.com/#helpers 请参阅。

hbs.registerHelper("IsSame", function(ProdType) {
 if(ProdType == "BlaBlaBla") {
   $('#HereText').append('<p>This is a test</p>');
    }
});

在模板中执行此操作

<div class="row">
   {{IsSame Product.ProdType}}
</div>

所以在这里你把你的值传递给一个帮助程序函数并进行比较。

我尝试@Matteo Conta的解决方案,但它不支持变量与文本的组合。所以我稍微调整了一下,以便于使用和更好地支持变量。

语法

{{#ifCond arg1 condition arg2}}
       True clause {{variable}}
{{else}}
       False clause {{variable}}
{{/ifCond}}

法典

Handlebars.RegisterHelper("ifCond", (writer, options, context, args) => {
    if (args.Length != 3)
    {
        writer.Write("ifCond:Wrong number of arguments");
        return;
    }
    if (args[0] == null || args[0].GetType().Name == "UndefinedBindingResult")
    {
        writer.Write("ifCond:args[0] undefined");
        return;
    }
    if (args[1] == null || args[1].GetType().Name == "UndefinedBindingResult")
    {
        writer.Write("ifCond:args[1] undefined");
        return;
    }
    if (args[2] == null || args[2].GetType().Name == "UndefinedBindingResult")
    {
        writer.Write("ifCond:args[2] undefined");
        return;
    }
    if (args[0].GetType().Name == "String")
    {
        var val1 = args[0].ToString();
        var val2 = args[2].ToString();
        switch (args[1].ToString())
        {
            case ">":
                if (val1.Length > val2.Length)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
            case "=":
            case "==":
                if (val1 == val2)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
            case "<":
                if (val1.Length < val2.Length)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
            case "!=":
            case "<>":
                if (val1 != val2)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
        }
    }
    else
    {
        var val1 = float.Parse(args[0].ToString());
        var val2 = float.Parse(args[2].ToString());
        switch (args[1].ToString())
        {
            case ">":
                if (val1 > val2)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
            case "=":
            case "==":
                if (val1 == val2)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
            case "<":
                if (val1 < val2)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
            case "!=":
            case "<>":
                if (val1 != val2)
                {
                    options.Template(writer, (object)context);
                }
                else
                {
                    options.Inverse(writer, (object)context);
                }
                break;
        }
    }
});

示例用法

var template = Handlebars.Compile(@"{{#ifCond arg1 '>' arg2}}{{arg1}} is greater than {{arg2}}{{else}}{{arg1}} is less than {{arg2}}{{/ifCond}}");
var data = new { arg1 = 2 , arg2 = 1};
var result = template(data);
Assert.Equal("2 is greater than 1", result);
data = new { arg1 = 1, arg2 = 2 };
result = template(data);
Assert.Equal("1 is less than 2", result);
template = Handlebars.Compile(@"{{#ifCond arg1 '<' arg2}}{{arg1}} is less than {{arg2}}{{else}}{{arg1}} is greater than {{arg2}}{{/ifCond}}");
data = new { arg1 = 2, arg2 = 1 };
result = template(data);
Assert.Equal("2 is greater than 1", result);
data = new { arg1 = 1, arg2 = 2 };
result = template(data);
Assert.Equal("1 is less than 2", result);
template = Handlebars.Compile(@"{{#ifCond arg1 '=' arg2}}{{arg1}} is eq to {{arg2}}{{else}}{{arg1}} is not eq to {{arg2}}{{/ifCond}}");
data = new { arg1 = 1, arg2 = 1 };
result = template(data);
Assert.Equal("1 is eq to 1", result);
data = new { arg1 = 2, arg2 = 1 };
result = template(data);
Assert.Equal("2 is not eq to 1", result);
template = Handlebars.Compile(@"{{#ifCond arg1 '!=' arg2}}{{arg1}} is not eq to {{arg2}}{{else}}{{arg1}} is eq to {{arg2}}{{/ifCond}}");
data = new { arg1 = 2, arg2 = 1 };
result = template(data);
Assert.Equal("2 is not eq to 1", result);
template = Handlebars.Compile(@"{{#ifCond str '!=' ''}}not empty{{else}}empty{{/ifCond}}");
var datastr = new { str = "abc" };
result = template(datastr);
Assert.Equal("not empty", result);
template = Handlebars.Compile(@"{{#ifCond str '==' ''}}empty{{else}}not empty{{/ifCond}}");
datastr = new { str = "" };
result = template(datastr);
Assert.Equal("empty", result);