如果比较,则为HandleBars .Net [英] HandleBars .Net If Comparision

查看:81
本文介绍了如果比较,则为HandleBars .Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Handlebars .NET for my mail templates so i generate template at server side width ASP.NET MVC. I need a comparision like this. But it doest't work? What can I do?

//Product.ProdType is a enum property 

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

推荐答案

我遇到了同样的问题,并创建了一个辅助函数"ifCond",该函数可用于数字数据类型和字符串.可以对其进行优化或扩展以与其他类型一起使用.

I had the same problem and I created a helper function "ifCond" that works with numeric data types and string. It can be optimized or expanded to work with other types.

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

这是一些解释用法的单元测试:

And here are some Unit test explaining the usage :

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

希望它会有所帮助,我也希望能实现更好的实现,更优雅,我的解决方案可以工作,但我认为可以用更简洁的方式来完成.

Hope it helps, and I also hope for a better implementation, more elegant, my solution works but I think it can be done in a more concise way.

这是一个在模板中使用Int32值的用法示例:

Here is a usage example with a Int32 value inside a template:

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

这是一个在模板内具有String值的用法示例:

And here is a usage example with a String value inside a template:

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

这篇关于如果比较,则为HandleBars .Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆