c# - HandleBars .Net If Comparision -


i use handlebars .net mail templates generate template @ server side width asp.net mvc. need comparision this. doest't work? can do?

//product.prodtype enum property   {{#if (product.prodtype=='blablabla')}} <p>this test</p> {{/if}} 

i had same problem , created helper function "ifcond" works numeric data types , string. can optimized or expanded work 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 unit test explaining 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 helps, , hope better implementation, more elegant, solution works think can done in more concise way.

here usage example int32 value inside template:

{{ifcond mycountvariable '>' 1 'more one' 'one'}} 

and here usage example string value inside template:

{{ifcond mystringvariable '!=' '' mystringvariable 'empty value'}} 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -