Node.js 参考';这';从handlebars在handlebars helper中的每个辅助对象在Ray块辅助对象中
我正在使用Node.js、express和 使用Node.js 参考';这';从handlebars在handlebars helper中的每个辅助对象在Ray块辅助对象中,node.js,handlebars.js,Node.js,Handlebars.js,我正在使用Node.js、express和 使用res.render()时,我将两个对象传递给我的把手模板,对象如下: var searchParams = {a: ['Apples']} var searchFields = {a: ['Apples', 'Pears', 'Oranges']} 然后,我希望使用searchFields在页面上创建复选框。当复选框变量位于相应的searchParams变量内时,我希望默认选中复选框 我正试图通过模板中的以下内容使用helper函数来实现这一点
res.render()
时,我将两个对象传递给我的把手模板,对象如下:
var searchParams = {a: ['Apples']}
var searchFields = {a: ['Apples', 'Pears', 'Oranges']}
然后,我希望使用searchFields
在页面上创建复选框。当复选框变量位于相应的searchParams
变量内时,我希望默认选中复选框
我正试图通过模板中的以下内容使用helper函数来实现这一点:
<form>
{{#each searchFields.a}}
<label>
<input name="only_a_test" value="{{this}}" type="checkbox"
{{#inArray searchParams.a this}}
checked
{{else}}
{{/inArray}}
>
{{this}}
</label>
{{/each}}
</form>
{{#每个搜索字段.a}
{{this}}
{{/每个}}
但是,这会引发错误:
无法读取未定义的属性“length”
TypeError: .../web/views/search.hbs: Cannot read property 'length' of undefined
at Object.indexOf (.../web/node_modules/handlebars-utils/index.js:82:31)
at String.helpers.inArray (.../web/node_modules/handlebars-helpers/lib/array.js:225:26)
at eval (eval at createFunctionContext (.../web/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:10:91)
at Object.prog [as inverse] (.../web/node_modules/handlebars/dist/cjs/handlebars/runtime.js:219:12)
at Object.utils.value (.../web/node_modules/handlebars-utils/index.js:237:50)
at String.helpers.eq (.../web/node_modules/handlebars-helpers/lib/comparison.js:170:15)
at eval (eval at createFunctionContext (.../web/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:5:84)
at prog (.../web/node_modules/handlebars/dist/cjs/handlebars/runtime.js:219:12)
at execIteration (.../web/node_modules/handlebars/dist/cjs/handlebars/helpers/each.js:51:19)
at Object.<anonymous> (.../web/node_modules/handlebars/dist/cjs/handlebars/helpers/each.js:61:13)
TypeError:…/web/views/search.hbs:无法读取未定义的属性“length”
位于Object.indexOf(…/web/node_modules/handlebar utils/index.js:82:31)
位于String.helpers.inArray(…/web/node_modules/handlebar helpers/lib/array.js:225:26)
在eval(在createFunctionContext(…/web/node_modules/handlebar/dist/cjs/handlebar/compiler/javascript compiler.js:254:23),:10:91处进行评估)
在Object.prog[as inverse](…/web/node_modules/handlebar/dist/cjs/handlebar/runtime.js:219:12)
在Object.utils.value(…/web/node_modules/handlebar utils/index.js:237:50)
位于String.helpers.eq(…/web/node_modules/handlebar helpers/lib/comparison.js:170:15)
评估时(在createFunctionContext(…/web/node_modules/handlebar/dist/cjs/handlebar/compiler/javascript compiler.js:254:23),:5:84评估时)
在prog(…/web/node_modules/handlebar/dist/cjs/handlebar/runtime.js:219:12)
在执行迭代时(…/web/node_modules/handlebar/dist/cjs/handlebar/helpers/each.js:51:19)
反对。(…/web/node_modules/handlebar/dist/cjs/handlebar/helpers/each.js:61:13)
我不太清楚发生了什么事,我怀疑在<代码> <数组> < /Cord>块帮助器中使用<代码> < <代码>——它可能没有看到底层字符串。
作为一种替代方法,您可以考虑编写自定义的“把手”。 如下图所示Handlebars.registerHelper("isInArray", function(array, value) {
if (array.indexOf(value) != -1) {
return "checked";
}
});
还是一种优化的方式
并在模板文件中将其称为
<input name="only_a_test" value="{{this}}" type="checkbox" {{isInArray ../b this}}>
。希望这有帮助
备选答案
正如OP所确认的,真正的问题是由于阵列被错误地访问。正确的代码是
{{#inArray ../searchParams.a this}}
您可以考虑更改数据结构。请参阅-我认为您不需要
{{{else}}
块在{{inArray…}}{/inArray}}
中尝试{{{inArray../searchParams.a this}
?它起作用了吗?你能解释一下为什么在车把模板中有。/b
?谢谢,我只是不理解。/
符号,也许我缺少了把手内部的一些东西,因为没有它它就无法工作。正如你在钢笔中看到的,数组名保留为a
和b
(var data={a:[“苹果”、“梨”、“橙子”],b:[“苹果”、“橙子”]};
)。由于它是在上下文{{{each a}}
中访问的,因此需要将路径指向父级。因此,使用。/
。我明白了,因此在{{each}
块中,您必须跳上一级才能访问b对象?另外,请参阅中的把手路径。它说“嵌套把手路径还可以包括../段,这些段根据父上下文评估其路径。”是的,你是对的。很高兴我能帮忙。您可以投票和/或将答案标记为正确答案,以帮助其他用户。非常感谢。
{{#inArray ../searchParams.a this}}