HandleBars .Net If Comparision

Yargicx picture Yargicx · Jun 19, 2015 · Viewed 10.5k times · Source

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

Answer

Matteo Conta picture Matteo Conta · Apr 6, 2016

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.

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

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

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

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