简体   繁体   English

将数组传递给 Handlebars 自定义助手

[英]Passing an Array to Handlebars Custom Helper

I am trying to pass an array of data to a custom helper with express-handlebars .我正在尝试使用express-handlebars将一组数据传递给自定义助手。 I can't get it to work.我无法让它工作。

I can pass in a string and I can pass in an array to an individual view, but I can't seem to pass in an array to a custom helper.我可以传入一个字符串,也可以将一个数组传递给单个视图,但我似乎无法将数组传递给自定义助手。 Here is my code:这是我的代码:

String Passed to Custom Helper (this works)传递给自定义助手的字符串(这有效)

// app.js
const hbs = expressHandlebars.create({
  extname: 'html',
  helpers: {
    foo: function () {
      return 'FOO!'
    }
})

// views/layout/main.html
<h2>{{ foo }}</h2>

// Result --> FOO! outputed as expected

Passing an Array to an Individual view (this also works)将数组传递给单个视图(这也有效)

// controllers/pageControllers.js
exports.getIndexPage = (req, res) => {
  res.render('index', {
    testList: ['one', 'two', 'three']
  })
}

// views/index.html
{{#each testList}}
<div>{{this}}</div>
{{/each}}

// Result --> one, two, three outputted as expected

Array Passed to a Custom Helper (this does NOT work)数组传递给自定义助手(这不起作用)

// app.js
const hbs = expressHandlebars.create({
  extname: 'html',
  helpers: {
    testList2: function () {
      return ['one', 'two', 'three']
    },
  }
})

// views/layout/main.html
{{#each testList2}}
  <div>{{this}}</div>
{{/each}}

// Result --> NOTHING.  No output, no error messages.

Do custom helpers not take arrays?自定义助手不带 arrays 吗? The same code works on an individual view.相同的代码适用于单个视图。 The same code works for a string on a custom helper.相同的代码适用于自定义助手上的字符串。 It just does not work for an array on a custom helper.它只是不适用于自定义助手上的数组。

If custom helpers do NOT accept arrays, then how can I get an array of data into my main layout file?如果自定义助手不接受 arrays,那么如何将一组数据放入我的主布局文件中? If it does take an array, then what I am doing wrong and how to get this to work?如果它确实需要一个数组,那么我做错了什么以及如何让它工作?

Thanks.谢谢。

According to documentation#helpers , - i'm not using express-handlebars myself .根据文档#helpers ,-我自己没有使用express-handlebars

A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space) Handlebars 助手调用是一个简单的标识符,后跟零个或多个参数(用空格分隔)

This means you call it with the first identifier and what follows is to be evaluated.这意味着您使用第一个标识符调用它,然后将对其进行评估。

The helper just takes in data and outputs a manipulated outcome to inject in the view.助手只是接收数据并输出一个经过处理的结果以注入到视图中。

Taking in data would look something like {{ foo exp1 exp2 }} which translates to {{foo(exp1,exp2)}} and the helper would look something like this.接收数据看起来像{{ foo exp1 exp2 }}转换为{{foo(exp1,exp2)}}并且助手看起来像这样。

 foo: function (exp1, exp2) {
      return `this is ${exp1} and ${exp2}`
    }

It's like invoking a regular function.这就像调用常规 function。

In your first example you call foo() (the helper function) which takes nothing in but outputs a string, and when activated you get the value return.在您的第一个示例中,您调用foo() (辅助函数),它什么都不接受,只输出一个字符串,当激活时,您会得到返回值。

In your second example you are using express to render a variable which is loopable on a view.在您的第二个示例中,您使用 express 来呈现在视图上可循环的变量。 nothing to do with handlebars instance creating.与车把实例创建无关。 (you are creating basicly an express res.locals.testList variable.) (您基本上是在创建一个快速res.locals.testList变量。)

In your third example you try to loop the helper function and not really invoking it.在您的第三个示例中,您尝试循环帮助程序 function 而不是真正调用它。 It's trying to do something like this:它试图做这样的事情:

for(let v of function() {}) {
 //...
}

This will give the error (probably silent in your case,) is not iterable这将给出错误(在你的情况下可能是沉默的) is not iterable

It's seems like you should manipulate the data array inside the helper function and then return a string or an html string (using Handlebars.Safestring as mentioned in the docs for escaping html).似乎您应该操作助手 function 中的数据数组,然后返回一个字符串或 html 字符串(使用 Z534F19BE86FF60EA96BBF80B90 文档中提到的Handlebars.Safestring )。 Then in your view just call it {{ testList2 }} which will out put a ready made HTML for the structure you were trying to do.然后在您看来,只需将其称为{{ testList2 }} ,它将为您尝试执行的结构提供现成的HTML

Hope this makes sense.希望这是有道理的。

That been said, There are more advanced ways of using helpers as mentioned in docs.话虽如此,文档中提到了使用助手的更高级方法。 that maybe could fit more for whatever it is you are trying to do.这可能更适合您尝试做的任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM