如何检查数组是否包含 PUG 模板中的字符串

pen*_*ngz 4 javascript string node.js express pug

我将 NodeJS 与 Express 和 PUG 视图引擎一起使用。

我正在尝试检查数组是否包含某个字符串。我尝试过内置的 javascript 方法,例如:

array.includes(str)
array.indexOf(str) > -1.
Run Code Online (Sandbox Code Playgroud)

然而,这些选项似乎都不起作用。如何检查数组是否包含 PUG 中的某个字符串?

if example_array.length > 0
  span() Array contains elements! // This works and appears on page
  if example_array.includes('example string') // This does not work
    span() Array includes example string! // This does not appear on page
Run Code Online (Sandbox Code Playgroud)

ale*_*ehr 6

如果要在模板中运行内联 JS,则必须将代码标记为无缓冲。 https://pugjs.org/language/code.html#unbuffered-code

  if example_array.length > 0
     span() Array contains elements! 
     - if (example_array.includes('example string'))
       span() Array includes example string! 
Run Code Online (Sandbox Code Playgroud)

注意第 3 行“if”前面的“-”。

由于这是一个文字 js 表达式,现在还需要括号。