0

我有一个在 Node、Express 和 express-handlebars 上运行的应用程序。该变量是分配给res.locals条件助手“ifCond”的查询的结果,例如这个。需要渲染的代码是部分的。问题是助手在使用变量时不起作用:

//getting the variable works
{{groups}} // Group2 

//works with standart block helper
{{#if groups}}1{{else}}2{{/if}} //  1  

//helper is working
{{#ifCond 5 "==" 5}}equal{{else}}not equal{{/ifCond}} //equal

//not working when using variable with helper
{{#ifCond groups "==" Group2}}equal{{else}}not equal{{/ifCond}} //not equal

什么会导致它不能与助手一起工作?谢谢你。

4

1 回答 1

1

您需要加上Group2引号,以便模板解析器将其视为字符串:

{{#ifCond groups "==" "Group2"}} 

因为您试图将字符串传递给您的助手。当您不在它周围加上引号时,handlebars 解析器会尝试将其解析为变量名称,就像groups它之前的那样。

于 2017-09-02T14:44:00.833 回答