我希望根据条件显示不同的HTML。
似乎没有比较这两个值,它总是显示第一个变体。如何将预定义值与JSON中的原始值进行比较,以便它可以正确执行?
{{#each this}}
{{#each visits}}
<div class="row">
{{#if variable_from_json }}
<div class="col-lg-2 col-md-2 col-sm-2">
<i class="fa fa-home"></i>
</div>
{{else}}
<div class="col-lg-2 col-md-2 col-sm-2">
<i class="fa fa-plus symbol-hospital"></i>
</div>
{{/if}}
</div>
{{/each}}
{{/each}}
JS代码
Handlebars.registerHelper('if', function (variable_from_json, options) {
if (variable_from_json === "M") {
return options.fn(this);
} else {
return options.inverse(this);
}
});
答案 0 :(得分:1)
比较==,&gt; =,||等值,&安培;&安培; 创建一个将处理所有案例的助手
Handlebars.registerHelper( "when",function(operand_1, operator, operand_2, options) {
var operators = {
'eq': function(l,r) { return l == r; },
'noteq': function(l,r) { return l != r; },
'gt': function(l,r) { return Number(l) > Number(r); },
'or': function(l,r) { return l || r; },
'and': function(l,r) { return l && r; },
'%': function(l,r) { return (l % r) === 0; }
}
, result = operators[operator](operand_1,operand_2);
if (result) return options.fn(this);
else return options.inverse(this);
});
在手柄文件中使用此运算符例如。 ==运算符
{{#when <operand1> 'eq' <operand2>}}
// do something here
{{/when}}
答案 1 :(得分:1)
这是Sunil More的答案,只是添加了<,> =和<=。
+ Apache 2.4.41
+ MariaDB 10.4.11
+ PHP 7.4.2 (VC15 X86 64bit thread safe) + PEAR
+ phpMyAdmin 5.0.1
+ OpenSSL 1.1.0g
+ ADOdb 518a
+ Mercury Mail Transport System v4.63 (not included in the portable version)
+ FileZilla FTP Server 0.9.41 (not included in the portable version)
+ Webalizer 2.23-04 (not included in the portable version)
+ Strawberry Perl 5.16.3.1 Portable
+ Tomcat 7.0.99
+ XAMPP Control Panel Version 3.2.4.
+ XAMPP mailToDisk 1.0 (write emails via PHP on local disk in <xampp>\mailoutput. Activated in the php.ini as mail default.)