【问题标题】:Handlebars C# if equals comparisonHandlebars C# if equals 比较
【发布时间】:2020-05-28 19:53:02
【问题描述】:

我正在尝试在 Handlebars .NET 上进行 if equals 比较。 {{#if xxx}} 正在使用布尔值。如何在 Handlebars .NET 中进行{{#if x = 'foo'}} 比较?

【问题讨论】:

  • 使用#if,您只能使用operators == and != 来测试布尔值真或假。 true 表示符号已定义,false 表示未定义。

标签: c# .net handlebarshelper handlebars.net


【解决方案1】:

您只能在此处使用数据中的布尔值,不能使用表达式:

{{#if foo}}
    {{helloMessage}}
{{/if}}

foo 将是一个布尔值:

var data = new
{
    helloMessage = "Hello world!",
    foo = true
};

.

如果你想处理类似表达式的行为,你也可以像这样注册一个块处理程序:

Handlebars.RegisterHelper("ifEq", (output, options, context, arguments) =>
{
    if (arguments[0] == arguments[1])
    {
        options.Template(output, context);
    }
    else
    {
        options.Inverse(output, context);
    }
});

并在您的模板中使用它:

{{#ifEq foo ""bar""}}
    {{body}}
{{/ifEq}}

但这在某种程度上与我对模板的理解相矛盾。

【讨论】:

  • 我知道它必须是布尔值。当我需要像等于、小于或大于这样的表达式时,我必须做什么?
  • 您无法在模板中处理此问题。您需要在数据中处理它:var x = "bar"; var data = new { helloMessage = "Hello world!", foo = x == "bar" };
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2019-03-13
  • 2023-03-11
  • 2013-02-04
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多