Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Handlebar辅助对象在Express中不使用全局变量_Node.js_Express_Handlebars.js_Express Handlebars - Fatal编程技术网

Node.js Handlebar辅助对象在Express中不使用全局变量

Node.js Handlebar辅助对象在Express中不使用全局变量,node.js,express,handlebars.js,express-handlebars,Node.js,Express,Handlebars.js,Express Handlebars,我有一个应用程序在Node、Express和Express Handlebar上运行。该变量是分配给res.locals和条件帮助器“ifCond”之类的查询的结果。需要呈现的代码是部分中的。问题在于,在使用变量时,帮助器不起作用: //getting the variable works {{groups}} // Group2 //works with standart block helper {{#if groups}}1{{else}}2{{/if}} // 1 //hel

我有一个应用程序在Node、Express和Express Handlebar上运行。该变量是分配给
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

是什么原因导致它无法与助手一起工作?谢谢。

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

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

因为您试图将字符串传递给助手。如果不在其周围加引号,Handlebar解析器会尝试将其解析为变量名,就像前面的
组一样

我认为您需要将
Group2
放在引号中:
{{{{{#ifCond groups”==“Group2”}
,因为您试图将字符串传递给您的助手。另外,
groups
真的是一个全局变量吗?还是将其作为选项对象的属性传递到
res.render()
?你不应该在页面呈现中使用全局变量。是的,谢谢,但是很奇怪,因为我没有使用“==”,所以不管它是不是字符串都要小心。我没有直接在res.render()中使用;将其传递给res.locals,以便它可以作为用户的属性全局可用。更确切地说,它只在函数的时间周期内(req,res)才可用。我做不到?这不关你的事。这是模板解析。如果不在其周围加引号,它会尝试查找具有该名称的变量,但没有。如果周围没有引号,它将被视为前面的
。查看帮助程序的代码,
==
如果将字符串与数字进行比较,仍然会产生影响。