c# - HandleBars .Net 比较

标签 c# asp.net-mvc handlebars.js mustache handlebars.net

我将 Handlebars .NET 用于我的邮件模板,因此我在服务器端宽度 ASP.NET MVC 生成模板。我需要这样的比较。但它不起作用?我能做些什么?

//Product.ProdType is a enum property 

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

最佳答案

我遇到了同样的问题,我创建了一个处理数字数据类型和字符串的辅助函数“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'}}

关于c# - HandleBars .Net 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30933956/

相关文章:

c# - Xamarin - TabbedPage 导航错误(页面不能有 parent )

c# - unity中如何使用接口(interface)

c# - .NET 4.5 中的序列化中断

asp.net-mvc - 路径中存在非法字符。 while 捆绑 javascript 文件

c# - 仅在满足特定条件时 throttle

html - 你怎么能做消失图像的背景?

asp.net-mvc - 哪个 ASP.NET MVC 验证库?

node.js - 如何在 Jade 之上的 Handlebars.js 模板中使用 Ember.js {{action}}

javascript - 如何将数据从 express 传递到我的 hbs View

javascript - 每个顶层都嵌套在 Handlebars 中