Handlebars.Net If 条件助手
Handlebars.Net If Condition Helper
我尝试编写一个 Handlebar.Net 助手,它的工作方式类似于 Equals。
Helper 应该像
一样使用
{{#eq name "Foo"}}
true
{{else}}
false
{{/eq}}
但我不知道如何实现这个助手。在 JS 中有 this 示例,但我找不到 C# 的示例。
我的第一枪是:
Handlebars.RegisterHelper("#eq", (output, context, data) =>
{
if (data.Length != 2)
output.WriteSafeString("false");
output.WriteSafeString(data[0].Equals(data[1]));
});
但这只是将 True 或 False 写入我的文件。
我找到了解决方案:
Handlebars.RegisterHelper(Equals, (output, options, context, data) =>
{
if (data.Length != 2)
options.Inverse(output, null);
if (data[0].Equals(data[1]))
options.Template(output, null);
else
options.Inverse(output, null);
});
Handlebars.RegisterHelper(LowerThan, (output, options, context, data) =>
{
IntegerOperation(LowerThan, ref output, ref options, ref data);
});
Handlebars.RegisterHelper(GreaterThan, (output, options, context, data) =>
{
IntegerOperation(GreaterThan, ref output, ref options, ref data);
});
Handlebars.RegisterHelper(LowerEquals, (output, options, context, data) =>
{
IntegerOperation(LowerEquals, ref output, ref options, ref data);
});
Handlebars.RegisterHelper(GreaterEquals, (output, options, context, data) =>
{
IntegerOperation(GreaterEquals, ref output, ref options, ref data);
});
private static void IntegerOperation(string operation, ref System.IO.TextWriter output, ref HelperOptions options, ref object[] data)
{
if (data.Length != 2)
{
options.Inverse(output, null);
return;
}
if (!int.TryParse(data[0].ToString(), out int leftValue))
{
options.Inverse(output, null);
return;
}
if (!int.TryParse(data[1].ToString(), out int rightValue))
{
options.Inverse(output, null);
return;
}
switch (operation)
{
case "lt":
if (leftValue < rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
case "le":
if (leftValue <= rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
case "gt":
if (leftValue > rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
case "ge":
if (leftValue >= rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
default:
break;
}
我尝试编写一个 Handlebar.Net 助手,它的工作方式类似于 Equals。 Helper 应该像
一样使用{{#eq name "Foo"}}
true
{{else}}
false
{{/eq}}
但我不知道如何实现这个助手。在 JS 中有 this 示例,但我找不到 C# 的示例。
我的第一枪是:
Handlebars.RegisterHelper("#eq", (output, context, data) =>
{
if (data.Length != 2)
output.WriteSafeString("false");
output.WriteSafeString(data[0].Equals(data[1]));
});
但这只是将 True 或 False 写入我的文件。
我找到了解决方案:
Handlebars.RegisterHelper(Equals, (output, options, context, data) =>
{
if (data.Length != 2)
options.Inverse(output, null);
if (data[0].Equals(data[1]))
options.Template(output, null);
else
options.Inverse(output, null);
});
Handlebars.RegisterHelper(LowerThan, (output, options, context, data) =>
{
IntegerOperation(LowerThan, ref output, ref options, ref data);
});
Handlebars.RegisterHelper(GreaterThan, (output, options, context, data) =>
{
IntegerOperation(GreaterThan, ref output, ref options, ref data);
});
Handlebars.RegisterHelper(LowerEquals, (output, options, context, data) =>
{
IntegerOperation(LowerEquals, ref output, ref options, ref data);
});
Handlebars.RegisterHelper(GreaterEquals, (output, options, context, data) =>
{
IntegerOperation(GreaterEquals, ref output, ref options, ref data);
});
private static void IntegerOperation(string operation, ref System.IO.TextWriter output, ref HelperOptions options, ref object[] data)
{
if (data.Length != 2)
{
options.Inverse(output, null);
return;
}
if (!int.TryParse(data[0].ToString(), out int leftValue))
{
options.Inverse(output, null);
return;
}
if (!int.TryParse(data[1].ToString(), out int rightValue))
{
options.Inverse(output, null);
return;
}
switch (operation)
{
case "lt":
if (leftValue < rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
case "le":
if (leftValue <= rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
case "gt":
if (leftValue > rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
case "ge":
if (leftValue >= rightValue)
options.Template(output, null);
else
options.Inverse(output, null);
break;
default:
break;
}